OpenCV findContours函数时的小发现

来源:互联网 发布:软件行业增值税税率 编辑:程序博客网 时间:2024/06/05 15:50

在应用opencv中findContours()函数与drawContours()函数进行滤波时,将检测到的面积小于某个值的轮廓使用drawContours()函数涂成黑色,此时在显示的图片上看不到该轮廓,但是此时的轮廓并没有在vector<vector<Point> > contours容器中删除。

实验过程:

原图为

经过一系列处理之后,利用下列代码将小于40的轮廓滤除,并将大于40的轮廓填充为白色

vector<vector<Point>> twocontours;       vector<Vec4i> twohierarchy;       findContours(result4,twocontours,twohierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);       for(inti = 0; i < (int)twocontours.size();i++)        {           if (int(twocontours.at(i).size())<= 40)              {                       drawContours(result4, twocontours, i, Scalar(0), CV_FILLED);              }          else             {              drawContours(result4,twocontours, i, Scalar(255), CV_FILLED);                 }       }

效果为:


此时将代码修改,在填充之后重新对counter进行填充

      

 vector<vector<Point> > twocontours;       vector<Vec4i> twohierarchy;       findContours(result4,twocontours,twohierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);        for(inti = 0; i < (int)twocontours.size();i++)        {           if (int(twocontours.at(i).size())<= 40)              {                       drawContours(result4, twocontours, i, Scalar(0), CV_FILLED);              }          else             {                      drawContours(result4, twocontours, i, Scalar(255), CV_FILLED);              }       }       for(inti = 0; i < (int)twocontours.size();i++)        {               drawContours(result4, twocontours, i,Scalar(255), CV_FILLED);       }

效果是,之前过滤掉的面积小于40的轮廓被重新填充回来,说明容器counter本身不会受到drawContours函数填充的影响。


2 0