matlab八邻域边界,用MATLAB进行边界跟踪的程序代码

void VessDibTrack(HDIB hdib,vector& pt) //八邻域

{

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;

}

}

}

//设置方向数组

// int direction[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=0&&NextPt.y

{

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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值