logo资料库

图像细化C++代码.doc

第1页 / 共18页
第2页 / 共18页
第3页 / 共18页
第4页 / 共18页
第5页 / 共18页
第6页 / 共18页
第7页 / 共18页
第8页 / 共18页
资料共18页,剩余部分请下载后查看
图像细化算法大全(1) #include "StdAfx.h" #include #include void beforethin(unsigned char *ip, unsigned char *jp, unsigned long lx, unsigned long ly) { unsigned long i,j; for(i=0; i0) jp[i*lx+j]=1; else jp[i*lx+j]=0; } } } ///////////////////////////////////////////////////////////////////////// //Hilditch 细化算法 //功能:对图象进行细化 //参数:image:代表图象的一维数组 // // // 无返回值 void ThinnerHilditch(void *image, unsigned long lx, unsigned long ly) { lx:图象宽度 ly:图象高度 char *f, *g; char n[10]; unsigned int counter; short k, shori, xx, nrn; unsigned long i, j; long kk, kk11, kk12, kk13, kk21, kk22, kk23, kk31, kk32, kk33, size; size = (long)lx * (long)ly; g = (char *)malloc(size); if(g == NULL) { printf("error in allocating memory!\n");
return; } f = (char *)image; for(i=0; i
kk11 = (i-1)*ly+j-1; kk12 = kk11 + 1; kk13 = kk12 + 1; kk21 = i*ly+j-1; kk22 = kk21 + 1; kk23 = kk22 + 1; kk31 = (i+1)*ly+j-1; kk32 = kk31 + 1; kk33 = kk32 + 1; if((g[kk12]&&g[kk21]&&g[kk23]&&g[kk32])!=0) continue; nrn = g[kk11] + g[kk12] + g[kk13] + g[kk21] + g[kk23] + g[kk31] + g[kk32] + g[kk33]; if(nrn <= 1) { f[kk22] = 2; continue; } n[4] = f[kk11]; n[3] = f[kk12]; n[2] = f[kk13]; n[5] = f[kk21]; n[1] = f[kk23]; n[6] = f[kk31]; n[7] = f[kk32]; n[8] = f[kk33]; n[9] = n[1]; xx = 0; for(k=1; k<8; k=k+2) { if((!n[k])&&(n[k+1]||n[k+2])) xx++; } if(xx!=1) { f[kk22] = 2; continue;
} if(f[kk12] == -1) { f[kk12] = 0; n[3] = 0; xx = 0; for(k=1; k<8; k=k+2) { if((!n[k])&&(n[k+1]||n[k+2])) xx++; } if(xx != 1) { f[kk12] = -1; continue; } f[kk12] = -1; n[3] = -1; } if(f[kk21]!=-1) { f[kk22] = -1; shori = 1; continue; } f[kk21] = 0; n[5] = 0; xx = 0; for(k=1; k<8; k=k+2) { if((!n[k])&&(n[k+1]||n[k+2])) { xx++; } } if(xx == 1)
{ f[kk21] = -1; f[kk22] = -1; shori =1; } else f[kk21] = -1; } } }while(shori); free(g); } ///////////////////////////////////////////////////////////////////////// //Pavlidis 细化算法 //功能:对图象进行细化 //参数:image:代表图象的一维数组 // // // 无返回值 void ThinnerPavlidis(void *image, unsigned long lx, unsigned long ly) { lx:图象宽度 ly:图象高度 char erase, n[8]; char *f; unsigned char bdr1,bdr2,bdr4,bdr5; short c,k,b; unsigned long i,j; long kk,kk1,kk2,kk3; f = (char*)image; for(i=1; i
} for(j=0, kk=(lx-1)*ly; j=1) bdr1|=0x80>>k; }
if((bdr1&0252)== 0252) continue; f[kk] = 2; b=0; for(k=0; k<=7; k++) { b+=bdr1&(0x80>>k); } if(b<=1) f[kk]=3; if((bdr1&0160)!=0&&(bdr1&07)!=0&&(bdr1&0210)==0) f[kk]=3; else if((bdr1&&0301)!=0&&(bdr1&034)!=0&&(bdr1&042)==0) f[kk]=3; else if((bdr1&0202)==0 && (bdr1&01)!=0) f[kk]=3; else if((bdr1&0240)==0 && (bdr1&0100)!=0) f[kk]=3; else if((bdr1&050)==0 && (bdr1&020)!=0) f[kk]=3; else if((bdr1&012)==0 && (bdr1&04)!=0) f[kk]=3; } } for(i=1; i
kk2 = kk + 1; n[4] = f[kk1]; n[0] = f[kk3]; kk1 = kk + ly -1; kk2 = kk1 + 1; kk3 = kk2 + 1; n[5] = f[kk1]; n[6] = f[kk2]; n[7] = f[kk3]; bdr1 = bdr2 =0; for(k=0; k<=7; k++) { if(n[k]>=1) bdr1|=0x80>>k; if(n[k]>=2) bdr2|=0x80>>k; } if(bdr1==bdr2) { f[kk] = 4; continue; } if(f[kk]!=2) continue; if((bdr2&0200)!=0 && (bdr1&010)==0 && ((bdr1&0100)!=0 &&(bdr1&001)!=0 || ((bdr1&0100)!=0 ||(bdr1 & 001)!=0) && (bdr1&060)!=0 &&(bdr1&06)!=0)) { } f[kk] = 4; else if((bdr2&040)!=0 && (bdr1&02)==0 && ((bdr1&020)!=0 && (bdr1&0100)!=0 || ((bdr1&020)!=0 || (bdr1&0100)!=0) && (bdr1&014)!=0 && (bdr1&0201)!=0)) { } f[kk] = 4;
分享到:
收藏