OPENCV algorithm for two-valued image refinement
Thinning algorithms usually have the same meaning as skeletal and skeleton algorithms, i.e. thin algorithms or skeleton algorithms. Although many image processing textbooks are not written in this way, specific reasons can be read in this paper, Louisa Lam, Seong-whan Lee, Ching Y. Suen, thinning methodologies-a, comprehensive Survey ", IEEE transactions on-pattern analysis and MACHINE INTELLIGENCE, vol. 9, September 1992, summarizes almost all classical refinement algorithms before 92.
function: void Cvthin (iplimage* src, iplimage* dst, int iterations=1)
Function: To refine the ipl_depth_8u type binary image
Parameter: SRC, original ipl_depth_8u type binary image
DST, target storage space 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. 3, pp. 236-239 , 1984.
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,j-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,j-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);
}
}
Use examples
#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;
Pass in 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,128,1,CV_THRESH_BINARY_INV)//Do two value processing, convert the image to 0, 1 format
Cvsaveimage ("C://threshold.bmp", ptmp,0);
Cvthin (ptmp,pdst,8)//refinement, further refinement by modifying iterations parameters
Cvnamedwindow ("src", 1);
Cvnamedwindow ("DST", 1);
Cvshowimage ("src", psrc);
Converts a two-valued image to grayscale to 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;
}