在许多时候,拍到的图像可能不如人意,需要把二值化后图像的边界不需要的白色区域去除,类似下面这样。因为我想找完整图像的轮廓和中心,那么边界上的不完整的图像,就是干扰图像。
思路就是取上下左右四条边界,然后遍历边界上的像素点,如果像素值是255,则存入vector point中,最后将这些点通过漫水填充法填充像素值等于0。 处理后效果如下:
最后附上这部分的代码,close就是预处理后输入的二值图:
//二值图边界图形去除
const int nr=close.rows;
const int nc=close.cols;
Mat edge[4];
edge[0] = close.row(0); //up
edge[1] = close.row(nr-1); //bottom
edge[2] = close.col(0); //left
edge[3] = close.col(nc-1); //right
std::vector<Point> edgePts;
const int minLength=1;
for(int i=0;i<4;++i)
{
std::vector<Point> line;
Mat_<uchar>::const_iterator iter = edge[i].begin<uchar>(); //当前像素
Mat_<uchar>::const_iterator nextIter = edge[i].begin<uchar>()+1; //下一个像素
while(nextIter!=edge[i].end<uchar>())
{
if(*iter==255)
{
if(*nextIter==255)
{
Point pt = iter.pos();
if(i==1)
pt.y = nr-1;
if(i==3)
pt.x = nc-1;
line.push_back(pt);
}
if(*nextIter!=255)
{
if(line.size()>minLength)
edgePts.push_back(line.at(line.size()/2));
line.clear();
}
}
++iter;
++nextIter;
}
}
for(int n =0; n<edgePts.size();++n)
floodFill(close,edgePts[n],0);//漫水填充法