利用种子填充法对二值图像进行连通域标记

申请删除 [复制链接]
查看4 | 回复0 | 2020-2-15 21:26:09 | |阅读模式
大致算法如下:设二值化图像A中,像素值为255的点是前景,为0的点是背景。A(x,y)为坐标(x,y)处的像素值,定义连通区域的label是一个1~254的整数,互不联通的区域有不同的label。初始化label=0。遍历图像的每个像素:1、如果像素值不等于255,则继续访问下一个元素。2、如果像素值为A(x,y)=255,则label++,当前值A(x,y)=label,并且a.检查其4个邻域,如果有属于前景的像素也给它赋予label值,并将它的坐标压栈。b.弹出栈顶坐标,重复a的过程,知道堆栈为空。此时,便找到了一个连通区域,该区域内的像素值被标记为label。intarea=0;//用于计算连通域的面积intleftBoundary=0;//连通域的左边界,即外接最小矩形的左边框,横坐标值,依此类推intrightBoundary=0;inttopBoundary=0;intbottomBoundary=0;Rectbox;//外接矩形框Featherfeather;featherList.clear();//清除数组dst.release();dst=src.clone();for(inti=0;irows;i++)uchar*pRow=dst.ptrucharfor(intj=0;jcols;j++)if(pRow[j]==255)area=0;labelValue++;//labelValue最大为254,最小为1.seed=Point(j,i);//Point(横坐标,纵坐标)dst.atuchar(seed)=labelValue;pointStack.push(seed);area++;leftBoundary=seed.x;rightBoundary=seed.x;topBoundary=seed.y;bottomBoundary=seed.y;while(!pointStack.empty())neighbor=Point(seed.x+1,seed.y);if((seed.x!=(cols-1))(dst.atuchar(neighbor)==255))dst.atuchar(neighbor)=labelValue;pointStack.push(neighbor);area++;if(rightBoundaryneighbor.x)rightBoundary=neighbor.x;neighbor=Point(seed.x,seed.y+1);if((seed.y!=(rows-1))(dst.atuchar(neighbor)==255))dst.atuchar(neighbor)=labelValue;pointStack.push(neighbor);area++;if(bottomBoundaryneighbor.y)bottomBoundary=neighbor.y;neighbor=Point(seed.x-1,seed.y);if((seed.x!=0)(dst.atuchar(neighbor)==255))dst.atuchar(neighbor)=labelValue;pointStack.push(neighbor);area++;if(leftBoundaryneighbor.x)leftBoundary=neighbor.x;neighbor=Point(seed.x,seed.y-1);if((seed.y!=0)(dst.atuchar(neighbor)==255))dst.atuchar(neighbor)=labelValue;pointStack.push(neighbor);area++;if(topBoundaryneighbor.y)topBoundary=neighbor.y;seed=pointStack.top();pointStack.pop();box=Rect(leftBoundary,topBoundary,rightBoundary-leftBoundary,bottomBoundary-topBoundary);rectangle(src,box,255);feather.area=area;feather.boundingbox=box;feather.label=labelValue;featherList.push_back(feather);returnlabelValue;intmain(intargc,char*argv[])Matsrc(imread("shape.jpg",0));if(src.empty())exit(-1);threshold(src,src,127,255,THRESH_BINARY);//二值化图像vectorFeatherfeatherList;//存放连通域特征Matdst;cout"连通域数量:"bwLabel(src,dst,featherList)endl;//为了方便观察,可以将label“放大”for(inti=0;idst.rows;i++)uchar*p=dst.ptrucharfor(intj=0;jdst.cols;j++)p[j]=30*p[j];cout"标号""t""面积"endl;for(vectorFeather::iteratorit=featherList.begin();itfeatherList.end();it++)coutit-label"t"it-areaendl;rectangle(dst,it-boundingbox,255);imshow("src",src);imshow("dst",dst);waitKey();destroyAllWindows();system("pause");return0;ZYNQSoC嵌入式开发(一):...XJT2015:[reply]diankui7151[/reply]rootfs是ext4格式,windows貌似无法访问,如果PC是Linux的就可以。你可以考到boot分区下,然后上电后去/media/xxx/BOOT找利用种子填充法对二值图像进行连通域...XJT2015:[reply]kittyyyyyyy[/reply]不会的,判断像素值是否为255就可以了,因为前景像素第一次被访问时就已经被更改了像素值。因此凡是检测到255的像素肯定是被第一次访问的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则