图像处理-----轮廓跟踪
文档一时没有找到,等找到了马上补上:
//轮廓跟踪// unsigned char* Contour(GPtr globals,unsigned char *Gray) { bool finish=false; int wn=0; //w为西,n为北,s为南,e为东 int n=0; int en=0; int w=0; int e=0; int ws=0; int s=0; int es=0;
w=-1; e=1; wn=-gStuff->imageSize.h-1; //计算各个方向指针的偏移 n=-gStuff->imageSize.h; en=-gStuff->imageSize.h+1; ws=gStuff->imageSize.h-1; s=gStuff->imageSize.h; es=gStuff->imageSize.h+1;
int start=NULL; //内存距离 int middle=NULL; int end=NULL; unsigned char* Gray2=NULL; unsigned char*p=NULL; int32 filterSize = gStuff->imageSize.v*gStuff->imageSize.h; Gray2=(unsigned char *)malloc( filterSize);
int i=0,j=0,bool1=0;
for(i=0;i<gStuff->imageSize.v;i++) //将所有非白的像素变为纯黑 { for(j=0;j<gStuff->imageSize.h;j++) { *(Gray2+(gStuff->imageSize.h*i)+j)=(BYTE)255; //将Gray2的内存全部写为白色,否则为灰色 if(*(Gray+(gStuff->imageSize.h*i)+j)!=255) *(Gray+(gStuff->imageSize.h*i)+j)=(BYTE)0; } }
for(i=0;i<gStuff->imageSize.v;i++) //取得首个像素的指针 { for(j=0;j<gStuff->imageSize.h;j++) { if(*(Gray+(gStuff->imageSize.h*i)+j)==0) { start=gStuff->imageSize.h*i+j; bool1=1; break; } } if(bool1==1) break; } p=Gray2+start; //*(Gray2+start)=(BYTE)0; i--;j--; //接着start的i,j继续循环
if(Judge(globals,Gray+start+e)) //此时找到的是start的e middle=start+e; else if(Judge(globals,Gray+start+es)) middle=start+es; else if(Judge(globals,Gray+start+s)) middle=start+s; else if(Judge(globals,Gray+start+ws)) middle=start+ws;
*(Gray2+middle)=(BYTE)0; while(!finish) { //此处的算法和书上不一样,是我自己想的查找顺序 if(Judge(globals,Gray+middle+wn)&& *(Gray+middle+wn)==0 && *(Gray2+middle+wn)==255) end=middle+wn; else if(Judge(globals,Gray+middle+n)&& *(Gray+middle+n)==0 && *(Gray2+middle+n)==255) end=middle+n; else if(Judge(globals,Gray+middle+en)&& *(Gray+middle+en)==0 && *(Gray2+middle+en)==255) end=middle+en; else if(Judge(globals,Gray+middle+e) && *(Gray+middle+e)==0 && *(Gray2+middle+e)==255) //此时找到的是middle的e end=middle+e; else if(Judge(globals,Gray+middle+es)&& *(Gray+middle+es)==0 && *(Gray2+middle+es)==255) end=middle+es; else if(Judge(globals,Gray+middle+s)&& *(Gray+middle+s)==0 && *(Gray2+middle+s)==255) end=middle+s; else if(Judge(globals,Gray+middle+ws)&& *(Gray+middle+ws)==0 && *(Gray2+middle+ws)==255) end=middle+ws; else if(Judge(globals,Gray+middle+w)&& *(Gray+middle+w)==0 && *(Gray2+middle+w)==255) end=middle+w; *(Gray2+end)=(BYTE)0; if(Gray+start==Gray+end) finish=true; else middle=end; }
memcpy(Gray, Gray2, filterSize); return Gray;
}