www.pudn.com > crackDection.zip > Thin.cpp, change:2013-07-31,size:4032b


#include <stdlib.h> 
#include <string.h> 
#include "cv.h" 
#include "highgui.h" 
#include "cxcore.h" 
#include "Thin.h" 
 
///////////////////////////////////////////////////////////////////////// 
//基于索引表的细化细化算法 
//功能:对图象进行细化 
//参数:lpDIBBits:代表图象的一维数组 
//      lWidth:图象高度 
//      lHeight:图象宽度 
//      无返回值 
 
bool ThiningDIBSkeleton (unsigned char* lpDIBBits, int lWidth, int lHeight) 
{	 
	//循环变量 
	long i; 
	long j; 
	long lLength; 
 
	unsigned char deletemark[256] = { 
		0,0,0,0,0,0,0,1,	0,0,1,1,0,0,1,1, 
		0,0,0,0,0,0,0,0,	0,0,1,1,1,0,1,1, 
		0,0,0,0,0,0,0,0,	1,0,0,0,1,0,1,1, 
		0,0,0,0,0,0,0,0,	1,0,1,1,1,0,1,1, 
		0,0,0,0,0,0,0,0,	0,0,0,0,0,0,0,0, 
		0,0,0,0,0,0,0,0,	0,0,0,0,0,0,0,0, 
		0,0,0,0,0,0,0,0,	1,0,0,0,1,0,1,1, 
		1,0,0,0,0,0,0,0,	1,0,1,1,1,0,1,1, 
		0,0,1,1,0,0,1,1,	0,0,0,1,0,0,1,1, 
		0,0,0,0,0,0,0,0,	0,0,0,1,0,0,1,1, 
		1,1,0,1,0,0,0,1,	0,0,0,0,0,0,0,0, 
		1,1,0,1,0,0,0,1,	1,1,0,0,1,0,0,0, 
		0,1,1,1,0,0,1,1,	0,0,0,1,0,0,1,1, 
		0,0,0,0,0,0,0,0,	0,0,0,0,0,1,1,1, 
		1,1,1,1,0,0,1,1,	1,1,0,0,1,1,0,0, 
		1,1,1,1,0,0,1,1,	1,1,0,0,1,1,0,0 
	};//索引表 
 
	unsigned char p0, p1, p2, p3, p4, p5, p6, p7; 
	unsigned char *pmid, *pmidtemp; 
	unsigned char sum; 
	int changed; 
	bool bStart = true; 
	lLength = lWidth * lHeight; 
	unsigned char *pTemp = (unsigned char *)malloc(sizeof(unsigned char) * lWidth * lHeight); 
	 
	//    P0 P1 P2 
	//    P7    P3 
	//    P6 P5 P4 
 
	while(bStart) 
	{ 
		bStart = false; 
		changed = 0; 
 
		//首先求边缘点(并行) 
		pmid = (unsigned char *)lpDIBBits + lWidth + 1; 
		memset(pTemp,  0, lLength); 
		pmidtemp = (unsigned char *)pTemp + lWidth + 1; 
		for(i = 1; i < lHeight -1; i++) 
		{ 
			for(j = 1; j < lWidth - 1; j++) 
			{ 
				if( *pmid == 0) 
				{ 
					pmid++; 
					pmidtemp++; 
					continue; 
				} 
 
				p3 = *(pmid + 1); 
				p2 = *(pmid + 1 - lWidth); 
				p1 = *(pmid - lWidth); 
				p0 = *(pmid - lWidth -1); 
				p7 = *(pmid - 1); 
				p6 = *(pmid + lWidth - 1); 
				p5 = *(pmid + lWidth); 
				p4 = *(pmid + lWidth + 1); 
				 
				sum = p0 & p1 & p2 & p3 & p4 & p5 & p6 & p7; 
				if(sum == 0) 
				{ 
					*pmidtemp = 1; 
	 
				} 
 
				pmid++; 
				pmidtemp++; 
			} 
			pmid++; 
			pmid++; 
			pmidtemp++; 
			pmidtemp++; 
		} 
		 
		//现在开始串行删除 
		pmid = (unsigned char *)lpDIBBits + lWidth + 1; 
		pmidtemp = (unsigned char *)pTemp + lWidth + 1; 
 
		for(i = 1; i < lHeight -1; i++) 
		{ 
			for(j = 1; j < lWidth - 1; j++) 
			{ 
				if( *pmidtemp == 0) 
				{ 
					pmid++; 
					pmidtemp++; 
					continue; 
				} 
 
				p3 = *(pmid + 1); 
				p2 = *(pmid + 1 - lWidth); 
				p1 = *(pmid - lWidth); 
				p0 = *(pmid - lWidth -1); 
				p7 = *(pmid - 1); 
				p6 = *(pmid + lWidth - 1); 
				p5 = *(pmid + lWidth); 
				p4 = *(pmid + lWidth + 1); 
				 
				p1 *= 2; 
				p2 *= 4; 
				p3 *= 8; 
				p4 *= 16; 
				p5 *= 32; 
				p6 *= 64; 
				p7 *= 128; 
 
				sum = p0 | p1 | p2 | p3 | p4 | p5 | p6 | p7; 
			//	sum = p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7; 
				if(deletemark[sum] == 1) 
				{ 
					*pmid = 0; 
					bStart = true; 
 
				} 
 
				pmid++; 
				pmidtemp++; 
			} 
 
			pmid++; 
			pmid++; 
			pmidtemp++; 
			pmidtemp++; 
		} 
 
	} 
 
	return true; 
} 
 
 
IplImage* Thin(IplImage* src) 
{ 
	cvThreshold(src,src,100,255,CV_THRESH_BINARY); 
	unsigned char* imagedata ; 
	imagedata = (unsigned char*)malloc(sizeof(char)*src->width*src->height); 
	int x , y; 
	for(y=0;y<src->height;y++) 
	{ 
		unsigned char* ptr  = (unsigned char*)(src->imageData + y*src->widthStep); 
		for(x=0;x<src->width;x++) 
		{ 
			imagedata[y*src->width+x] = ptr[x] > 0 ? 1 : 0; 
 
		} 
 
	} 
 
	ThiningDIBSkeleton(imagedata,src->width,src->height); 
	for(y=0;y<src->height;y++) 
	{ 
		unsigned char* ptr  = (unsigned char*)(src->imageData + y*src->widthStep); 
		for(x=0;x<src->width;x++) 
		{ 
			ptr[x] = imagedata[y*src->width + x]>0? 255 : 0; 
 
		} 
 
	} 
	cvNamedWindow("图像细化"); 
	cvShowImage("图像细化" , src); 
	cvWaitKey(0); 
	free(imagedata); 
	return src; 
 
}