opencv : 边缘区域去除

来源:互联网 发布:win10美化成mac 编辑:程序博客网 时间:2024/04/29 08:44

            边缘区域的定义:图像上的一个区域位于边缘。如下图所示:

 

标号1 为一个边缘区域

 现在希望可以将图像中的边缘区域去除。一个简单的思路如下:

           遍历图像上下左右四条边界上的像素。设置一个计数器和一个最小边界阈值。当边界满足要求的像素个数大于阈值,就作为一个待处理区域。并选取其中一个坐标点作为种子点进行满水填充为0。


代码如下:

    Mat src=imread("F:/test.JPG");    imshow("src",src);    //灰度化    Mat grayImage;    cvtColor(src,grayImage,CV_BGR2GRAY);    //二值化原图像    Mat thresImage=Mat::zeros(grayImage.rows,grayImage.cols,CV_8UC1);    threshold(grayImage,thresImage,127,255,THRESH_BINARY);    imshow("thresImage",thresImage);    const int nr=thresImage.rows;    const int nc=thresImage.cols;    Mat edge[4];    edge[0] = thresImage.row(0);    //up    edge[1] = thresImage.row(nr-1); //bottom    edge[2] = thresImage.col(0);    //left    edge[3] = thresImage.col(nc-1); //right    std::vector<Point> edgePts;    const int minLength=std::min(nr,nc)/4;    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(thresImage,edgePts[n],0);//漫水填充法

 效果:

         

                                     

0 0
原创粉丝点击