8-邻域边界跟踪算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C++
利用点的八邻域信息,选择下一个点作为边界点,这个算法需要选择一个开始点,可以选择图像上是目标点,在最上,最左的点。然后查看它的八邻域的点,从右下方45°的位置开始寻找,如果是目标点,将沿顺时针90°作为下一次寻找的方向,如果不是,则逆时针45°继续寻找,一旦找到重复上面的过程。
void VessDibTrack(HDIB hdib,vector
{
unsigned char *lpSrc;
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)hdib);
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
LPSTR lpDIBBits=::FindDIBBits (lpDIB);
long lLineBytes = WIDTHBYTES(cxDIB * 8); //计算图像每行的字节数
//寻找开始点,最右,最下
//先行,后列
int i,j;
POINT startPt,currPt;
for (i = 0;i { for (j = 0;j { lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB -1 - i) + j;if (*lpSrc==0) //是黑点 startPt.x = i; startPt.y = j; //停止条件 i = cyDIB; j = cxDIB; } } } //设置方向数组 //intdirection[8][2]={{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}};int direction[8][2] = {{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};int startDirect ; BOOL FindStartPoint = FALSE; BOOL FindPoint = FALSE; //开始跟踪 currPt.x = startPt.x; currPt.y = startPt.y; POINT NextPt ; int NextPtValue; startDirect = 0; int cnt= 0; while(!FindStartPoint&&cnt<100000) FindPoint = FALSE; pt.push_back(currPt); while(!FindPoint) { //沿预定方向扫描,寻找下一个点 NextPt.x = currPt.x + direction[startDirect][1]; NextPt.y = currPt.y + direction[startDirect][0]; lpSrc = (unsigned char*)lpDIBBits + lLineBytes * (cyDIB -1 - NextPt.x) +NextPt.y; //NextPtValue = GetPiexValue(hdib,NextPt.x,NextPt.y); NextPtValue = * lpSrc; if (NextPtValue == 0&&NextPt.x>=0&&NextPt.x { FindPoint = TRUE; currPt.x = NextPt.x; currPt.y = NextPt.y; if (currPt.x == startPt.x&&currPt.y == startPt.y) { FindStartPoint = TRUE; } startDirect = startDirect-2; if(startDirect<0) startDirect = startDirect+8; } else { startDirect = startDirect+1; if (startDirect>=8) startDirect = startDirect-8; } } cnt ++; } ::GlobalUnlock((HGLOBAL) hdib); }