没有使用其他的库,C++实现的连通域算法
typedef struct poi
{
int x;
int y;
};
void con_sub(BYTE *data,int width,int height,std::queue<poi> &hole,int x,int y)
{
if (x<0 || x>=height || y<0 || y>=width)
{
return ;
}
if (data[x*width+y]>=0 && data[x*width+y]<=60)
{
poi buf;
buf.x=x;
buf.y=y;
hole.push(buf);
data[x*width+y]=255;
}
}
void con(BYTE *data,int width,int height)
{
std::queue<poi> hole;
int num=0;
for (int i=0;i<height;++i)
{
for (int j=0;j<width;++j)
{
if ( data[i*width+j]>=0 && data[i*width+j]<=60 )
{
poi buf;
buf.x=i;
buf.y=j;
hole.push(buf);
num++;
}
while(!hole.empty())
{
poi buf=hole.front();
hole.pop();
con_sub(data,width,height,hole,buf.x+1,buf.y+1);
con_sub(data,width,height,hole,buf.x+1,buf.y-1);
con_sub(data,width,height,hole,buf.x+1,buf.y);
con_sub(data,width,height,hole,buf.x-1,buf.y+1);
con_sub(data,width,height,hole,buf.x-1,buf.y-1);
con_sub(data,width,height,hole,buf.x-1,buf.y);
con_sub(data,width,height,hole,buf.x,buf.y+1);
con_sub(data,width,height,hole,buf.x,buf.y-1);
}
}
}
}