Opencv Algorithm for refining Binary Images

Source: Internet
Author: User

From: http://blog.csdn.net/byxdaz/archive/2010/06/02/5642669.aspx

 

The refinement algorithm is usually the same as the skeleton and skeleton algorithms, that is, the thin algorithm or the Skeleton Algorithm. Although this is not the case in many image processing textbooks, you can refer to this article for specific reasons: Louisa Lam, Seong-whan Lee, Ching y. suen, "thinning methodologies-a comprehensive survey", IEEE Transactions on Pattern Analysis and machine intelligence, vol. 14, No. 9. September 1992, which summarizes almost all the classic refinement algorithms earlier than 92 years ago.

Function: void cvthin (iplimage * SRC, iplimage * DST, int iterations = 1)
Function: refine the ipl_depth_8u binary image.
Parameter: SRC, original ipl_depth_8u Binary Image
DST, the destination storage space, which must be allocated in advance and consistent with the original image size type
Iterations, number of iterations
References: T. y. zhang and C. y. suen, "a fast parallel algorithm for thinning digital patterns," comm. ACM, vol. 27, No. 3, pp. 236-239,198 4.

Void cvthin (iplimage * SRC, iplimage * DST, int iterations = 1)
{
Cvsize size = cvgetsize (SRC );

Cvcopy (SRC, DST );
Int n = 0, I = 0, j = 0;
For (n = 0; n <iterations; n ++)
{
Iplimage * t_image = cvcloneimage (DST );
For (I = 0; I <size. height; I ++)
{
For (j = 0; j <size. width; j ++)
{
If (cv_image_elem (t_image, byte, I, j) = 1)
{
Int ap = 0;
Int P2 = (I = 0 )? 0: cv_image_elem (t_image, byte, I-1, J );
Int P3 = (I = 0 | j = size. Width-1 )? 0: cv_image_elem (t_image, byte, I-1, J + 1 );
If (P2 = 0 & P3 = 1)
{
AP ++;
}
Int P4 = (j = size. Width-1 )? 0: cv_image_elem (t_image, byte, I, j + 1 );
If (P3 = 0 & P4 = 1)
{
AP ++;
}
Int P5 = (I = size. Height-1 | j = size. Width-1 )? 0: cv_image_elem (t_image, byte, I + 1, J + 1 );
If (P4 = 0 & P5 = 1)
{
AP ++;
}
Int P6 = (I = size. Height-1 )? 0: cv_image_elem (t_image, byte, I + 1, J );
If (P5 = 0 & P6 = 1)
{
AP ++;
}
Int P7 = (I = size. Height-1 | j = 0 )? 0: cv_image_elem (t_image, byte, I + 1, J-1 );
If (P6 = 0 & P7 = 1)
{
AP ++;
}
Int P8 = (j = 0 )? 0: cv_image_elem (t_image, byte, I, J-1 );
If (P7 = 0 & P8 = 1)
{
AP ++;
}
Int P9 = (I = 0 | j = 0 )? 0: cv_image_elem (t_image, byte, I-1 );
If (P8 = 0 & P9 = 1)
{
AP ++;
}
If (P9 = 0 & p2 = 1)
{
AP ++;
}
If (P2 + P3 + P4 + P5 + P6 + P7 + P8 + P9)> 1 & (P2 + P3 + P4 + P5 + P6 + P7 + P8 + P9) <7)
{
If (AP = 1)
{
If (! (P2 & P4 & P6 ))
{
If (! (P4 & P6 & P8 ))
{
Cv_image_elem (DST, byte, I, j) = 0;
}
}
}
}

}
}
}
Cvreleaseimage (& t_image );
T_image = cvcloneimage (DST );
For (I = 0; I <size. height; I ++)
{
For (Int J = 0; j <size. width; j ++)
{
If (cv_image_elem (t_image, byte, I, j) = 1)
{
Int ap = 0;
Int P2 = (I = 0 )? 0: cv_image_elem (t_image, byte, I-1, J );
Int P3 = (I = 0 | j = size. Width-1 )? 0: cv_image_elem (t_image, byte, I-1, J + 1 );
If (P2 = 0 & P3 = 1)
{
AP ++;
}
Int P4 = (j = size. Width-1 )? 0: cv_image_elem (t_image, byte, I, j + 1 );
If (P3 = 0 & P4 = 1)
{
AP ++;
}
Int P5 = (I = size. Height-1 | j = size. Width-1 )? 0: cv_image_elem (t_image, byte, I + 1, J + 1 );
If (P4 = 0 & P5 = 1)
{
AP ++;
}
Int P6 = (I = size. Height-1 )? 0: cv_image_elem (t_image, byte, I + 1, J );
If (P5 = 0 & P6 = 1)
{
AP ++;
}
Int P7 = (I = size. Height-1 | j = 0 )? 0: cv_image_elem (t_image, byte, I + 1, J-1 );
If (P6 = 0 & P7 = 1)
{
AP ++;
}
Int P8 = (j = 0 )? 0: cv_image_elem (t_image, byte, I, J-1 );
If (P7 = 0 & P8 = 1)
{
AP ++;
}
Int P9 = (I = 0 | j = 0 )? 0: cv_image_elem (t_image, byte, I-1 );
If (P8 = 0 & P9 = 1)
{
AP ++;
}
If (P9 = 0 & p2 = 1)
{
AP ++;
}
If (P2 + P3 + P4 + P5 + P6 + P7 + P8 + P9)> 1 & (P2 + P3 + P4 + P5 + P6 + P7 + P8 + P9) <7)
{
If (AP = 1)
{
If (P2 * P4 * P8 = 0)
{
If (P2 * P6 * P8 = 0)
{
Cv_image_elem (DST, byte, I, j) = 0;
}
}
}
}
}

}

}
Cvreleaseimage (& t_image );
}

}

// Example

# Include "cxcore. H"
# Include "cv. H"
# Include "highgui. H"

Int main (INT argc, char * argv [])
{
If (argc! = 2)
{
Return 0;
}
Iplimage * psrc = NULL, * pdst = NULL, * pTMP = NULL;

// Input a grayscale image
Psrc = cvloadimage (argv [1], cv_load_image_grayscale );
If (! Psrc)
{
Return 0;
}
PTMP = cvcloneimage (psrc );
Pdst = cvcreateimage (cvgetsize (psrc), psrc-> depth, psrc-> nchannels );
Cvzero (pdst );
Cvthreshold (psrc, pTMP, 1, cv_thresh_binary_inv); // convert the image to the format
// Cvsaveimage ("C: // threshold.bmp", pTMP, 0 );
Cvthin (pTMP, pdst, 8); // refined by modifying the iterations Parameter
Cvnamedwindow ("src", 1 );
Cvnamedwindow ("DST", 1 );
Cvshowimage ("src", psrc );
// Convert a binary image to a grayscale image for display
Int I = 0, j = 0;
Cvsize size = cvgetsize (pdst );
For (I = 0; I <size. height; I ++)
{
For (j = 0; j <size. width; j ++)
{
If (cv_image_elem (pdst, uchar, I, j) = 1)
{
Cv_image_elem (pdst, uchar, I, j) = 0;
}
Else
{
Cv_image_elem (pdst, uchar, I, j) = 255;
}
}
}
// Cvsaveimage ("C: // thin.bmp", pdst );
Cvshowimage ("DST", pdst );
Cvwaitkey (0 );
Cvreleaseimage (& psrc );
Cvreleaseimage (& pdst );
Cvreleaseimage (& pTMP );
Cvdestroywindow ("src ");
Cvdestroywindow ("DST ");
Return 0;
}

 

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.