求二值图像目标区域并求各个区域的面积源码?
我在用8链码寻目标图像连通区域并计算面积时,程序编译通过,但是在处理时总是跳不循环。请各位过路高手帮忙指点一下!不胜感激!
代码如下:
//八个方向和起始扫描方向
int Direction[8][2]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
int BeginDirect=0;
StartPoint.Height=0;
StartPoint.Width =0;
col=(unsigned char)0;
for(k=0;k<100;k++)
{
biFindStartPoint=false;
//*(area+col)=0;
for (j = StartPoint.Height;j < lHeight && !biFindStartPoint;j++)
{
for(i =StartPoint.Width;i < lWidth && !biFindStartPoint;i++)
{
// 指向源图像倒数第j行,第i个象素的指针
lpSrc = (char *)lpDIBBits + lLineBytes * j + i;
lpDst = (char *)lpNewDIBBits+ lLineBytes * j + i;
pixel = (unsigned char)*lpSrc;
if(((unsigned char)*lpDst)==255)
{
if(pixel == 0)
{
//找到初始点
biFindStartPoint = true;
StartPoint.Height = j;
StartPoint.Width = i;
*lpDst = (unsigned char)col;
//初步判断
nw = (unsigned char)*(lpSrc + 6*lLineBytes +6);
n = (unsigned char)*(lpSrc + 3*lLineBytes +3);
ne = (unsigned char)*(lpSrc + 9*lLineBytes +9);
if(nw+n+ne!=0)biFindStartPoint = false;
}
}
}
}
CurrentPoint.Height=StartPoint.Height;
CurrentPoint.Width =StartPoint.Width ;
//区域连通求面积
count=1;
biFindStartPoint = false;
while( !biFindStartPoint)
{
BeginDirect=0;
biFindPoint= false;
while(!biFindPoint)
{
//沿扫描方向查看一个像素
lpSrc = (char *)lpDIBBits + lLineBytes * ( CurrentPoint.Height + Direction[BeginDirect][1])
+ (CurrentPoint.Width + Direction[BeginDirect][0]);
pixel = (unsigned char)*lpSrc;
//if the memory point is never marked,
lpDst = (char *)lpNewDIBBits + lLineBytes * ( CurrentPoint.Height + Direction[BeginDirect][1])
+ (CurrentPoint.Width + Direction[BeginDirect][0]);
if(((unsigned char)(*lpDst)==255))
{
if(pixel == 0)
{
biFindPoint = true;
CurrentPoint.Height = CurrentPoint.Height + Direction[BeginDirect][1];
CurrentPoint.Width = CurrentPoint.Width + Direction[BeginDirect][0];
*lpDst = (unsigned char)col;
count++;
*(area+col)=count;
}
else
{
BeginDirect++;
if(BeginDirect == 8)
{
BeginDirect = 0;
//如果当前点周围的八个点都是已经找到过的点,结束该区域循环
lpDst = (char *)lpNewDIBBits + lLineBytes * CurrentPoint.Height + CurrentPoint.Width;
if(((unsigned char)*(lpDst - lLineBytes-1 )+(unsigned char)*(lpDst - lLineBytes )+(unsigned char)*(lpDst - lLineBytes +1)
+(unsigned char)*(lpDst -1)+(unsigned char)*(lpDst )+(unsigned char)*(lpDst + 1)
+(unsigned char)*(lpDst + lLineBytes-1 )+(unsigned char)*(lpDst + lLineBytes )+(unsigned char)*(lpDst + lLineBytes+1))==9*col)
{
biFindStartPoint=true;
biFindPoint = true;
}
}
}
}
}//
}
col++;
}