Binary Image Contour Tracking source code [maffuzi]

Source: Internet
Author: User

// Image. cpp 98-8-8
// Process the vector image corresponding to the contour Image
// Use the contour tracing method for processing

# Include "stdafx. H"
# Include "stdio. H"
# Include "osm2view. H"
# Include "image. H"
# Include "Global. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

# Define frame 50
# Define max_lentimes 2
# Define max_point_per_circle 20000
# Define min_gap 15
# Define line_len 14 // must be even
# Define dec_count 1 // The Times of continuely decreasing
# Define max_den 8 // The max distance between two inflections
# Define err_mem 10
# Define err_file 11
# Define err_bmp 12
# Define err_pixel 13
# Define err_size 14
# Define err_unknow 15
# Define m_pi 3.1415926
// The current circle and the number of point in current circle
Int numcnt, totalpoint;
Float skew_k, dpm_k; // temp for adjust scanor
Outpoint: outpoint (float _ x, float _ y)
{
// First: Adjust
Y = _ y * yscale;
X = _ x * XScale + y * skew_k;
// Second: logical to world
Y * = dpm_k;
X * = dpm_k;
}
/*-------------------------------------------------------*/
Image: image ()
{
Sizex = sizey = numcontour = 0; // initialize the image size and number of outlines.
Numjidian = numorphan = numhole = 0; // node, isolated point, number of holes initialized
Scale = 0.2; // pixel Ratio
View = NULL;
PDC = NULL;
P = NULL; // pointer to the white point of the pixel array
}
// Release the memory
Void image: flush ()
{
// Release the node, memory occupied by pixels
For (INT I = numcontour-1; I> = 0; I --){
Delete [] CN;
Delete [] cy;
Delete [] cx;
}
// Delete by image size
For (I = 0; I <sizey; I ++)
Delete [] P [sizey-I-1];
If (sizey> 0) Delete [] P;
// Assign an initial value
Scale = 0.2;
Sizex = sizey = 0;
Numcontour = numjidian = numorphan = numhole = 0;
View = NULL;
PDC = NULL;
}

// Load the contour Image
Int image: loadcnt (lpstr name)
{
Int J, K, nump;
Dword wd;
// Open the profile and read the element
File * CNT = fopen (name, "rb ");
If (! CNT) return err_file;
Fread (& WD, sizeof (Word), 1, CNT );
Sizex = WD;
Fread (& WD, sizeof (Word), 1, CNT );
Sizey = WD;
Fread (& WD, sizeof (Word), 1, CNT );
DPM = WD;
// Adapt to the current window display
Fittowindow ();

// Allocate memory for the pixels to be displayed
P = new pack_image [sizey];
For (j = 0; j <sizey; j ++ ){
// Read a row or column
Fread (& WD, sizeof (Word), 1, CNT );
Nump = WD;
// Allocate memory for the Dot Array
// Fread (& nump, sizeof (INT), 1, CNT );
P [J] = new ptarray [nump + 1];
P [J] [0]. Col = nump; // calculate the number of columns in the black spot.
// Read the vertex array and draw the contour Pixel
For (k = 0; k <nump; k ++ ){
Fread (& WD, sizeof (Word), 1, CNT );
P [J] [k + 1]. Col = WD;
P [J] [k + 1]. Order =-1;
// If (PDC) PDC-> setpixel (P [J] [k + 1]. Col * scale, J * scale, RGB (0, 0, 0 ));
If (PDC) PDC-> setpixel (J * scale, P [J] [k + 1]. Col * scale, RGB (0, 0, 0 ));
}
}
// Read the ratio and angle of the contour
Fread (& XScale, sizeof (float), 1, CNT );
Fread (& yscale, sizeof (float), 1, CNT );
Fread (& skew, sizeof (float), 1, CNT );
// Close the profile file and read it into the Array
Fclose (CNT );
Return 0;
}
// The CNT output is not rotated by 90, because it cannot be determined whether to turn it during the next read.
// Save BMP images as profile files and vector files
Void image: savebmptocnt (lpstr name)
{
// Assign a file name with the suffix pnt
Char * P = strrchr (name, & #39;. & #39 ;);
If (P! = NULL) * p = 0;
Strcat (name, ". PNT ");
/*. PNT file format (Binary)
(Sizex, sizey, DPM)
(Number of points in first row-int)
(X-coord. of point-1-int)
......
(X-coord. of point-n-int)
......
(Number of points in last row-int)
(X-coord. of point-1-int)
......
(X-coord. of point-n-int)
(XScale, yscale, skew)
*/
Int J, K, nump;
Word WD;

// Open a file in the memory and name it WB
File * CNT = fopen (name, "WB ");
If (! CNT) return; // err_file
// Write data
WD = sizex;
Fwrite (& WD, sizeof (Word), 1, CNT );
WD = sizey;
Fwrite (& WD, sizeof (Word), 1, CNT );
WD = DPM;
Fwrite (& WD, sizeof (Word), 1, CNT );
// Write Coordinate
For (j = 0; j <sizey; j ++ ){
Nump = P [J] [0]. Col;
WD = nump;
Fwrite (& WD, sizeof (Word), 1, CNT );
For (k = 0; k <nump; k ++ ){
WD = P [J] [k + 1]. Col;
Fwrite (& WD, sizeof (Word), 1, CNT );
}
}

// Write ratio, Angle
Fwrite (& XScale, sizeof (float), 1, CNT );
Fwrite (& yscale, sizeof (float), 1, CNT );
Fwrite (& skew, sizeof (float), 1, CNT );
// Close the file and release the memory
Fclose (CNT );
}
// Image conversion, from image to image
Void image: Transfer (char * PP [], int start, int count)
{
Int I, J, K, L, M, index, PTR, * Buf;
// Temporary store the black pixel in one line
Buf = new int [sizex];
// Spot erasing Removal
// Int spotpass = 2;
// Gbang gdadi guser

/*
If (gbang = 1)
{
Spotpass = 0;
Dilationpass = 0;
}

Else if (gdadi = 1)
{
Spotpass = 1;
Dilationpass = 1;
}

Else if (guser = 1)
{
Spotpass;
Dilationpass;
}
*/
For (k = 0; k <spotpass; k ++ ){
For (I = 1; I <count + 1; I ++)
For (j = 1; j <sizex-1; j ++ ){
Int meanvalue = PP [I-1] [J-1] + PP [I-1] [J] + PP [I-1] [J + 1] +
PPJ-1 + PP[J + 1] +
PP [I + 1] [J-1] + PP [I + 1] [J] + PP [I + 1] [J + 1];
// If the difference between the point-value and mean-value is
// Greater than 0.5, then reverse this point
If (PP[J] = black ){
If (meanvalue <4) pp[J] = white;
}
Else {
If (meanvalue> 4) pp[J] = black;
}
}
}
// Image dilating image Expansion
For (k = 0; k <dilationpass; k ++ ){
For (I = 1; I <count-1; I ++)
For (j = 1; j <sizex-1; j ++ ){
If (PP[J] = black ){
For (L =-1; L <= 1; l ++)
For (M =-1; m <= 1; m ++)
If (PP [I + L] [J + M] = white)
PP [I + L] [J + M] = black + 2;
}
}
For (I = 0; I <count + 2; I ++)
For (j = 0; j <sizex; j ++)
If (PP[J] = black + 2) pp[J] = black;
}
// Extracting Contour: extract the contour and process it in contour mode.
// If all of 4-neighbourhood is black then set this point to white
// This process only for the method "outline"
If (Method = 0) {// outline
For (I = 1; I <count + 1; I ++)
For (j = 1; j <sizex-1; j ++ ){
If (PP[J] = white ){
J ++;
Continue;
}
If (PP [I-1] [J]> = Black & PP[J-1]> = Black &&
PP [I + 1] [J]> = Black & PP[J + 1]> = black)
PP[J] = black + 1; // This point will be deleted
}
}
If (count <frame-2) // This is the last block
Count + = 2;
// Scan the line to extract Black Point pixels and coexist as Ppoints
For (j = start-2, I = 0; I <count; I ++, J ++ ){
// Scan one line, extract the Black Point and store it to P
For (k = PTR = 0; k <sizex; k ++)
If (PP[K] = black) BUF [PTR ++] = K;
// The image is displaied inversely reverse display image
Index = J;
P [Index] = new ptarray [PTR + 1];
// The unit 0 store the number of point, that stored in the structure P
// Store points and set vector points
P [Index] [0]. Col = PTR;
For (k = 0; k <PTR; k ++ ){
P [Index] [k + 1]. Col = Buf [k];
P [Index] [k + 1]. Order =-1;
// If (PDC) PDC-> setpixel (BUF [k] * scale, Index * scale, RGB (0, 0, 0 ));
If (PDC) PDC-> setpixel (Index * scale, Buf [k] * scale, RGB (0, 0, 0 ));
}
// Record the number of black points
// Total + = PTR;
}
Delete [] Buf;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.