找轮廓,清除不满足条件的轮廓,将满足条件的轮廓内的图像拷贝出来

找轮廓,清除不满足条件的轮廓,将满足条件的轮廓内的图像拷贝出来

Mat  img = imread("0.jpg");

Mat res;

cvtColor(img,res,CV_BGR2GRAY);


vector<vector<Point> > contours_set;//保存轮廓提取后的点集及拓扑关系

findContours(res,contours_set,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); //寻找轮廓

vector<vector<Point> >::iterator iter = contours_set.begin() ;
for(; iter!= contours_set.end(); )
{
//cout << iter->size() << endl;
Rect rect = boundingRect(*iter );
if (rect.area() > 50 && rect.width < 50 && rect.height <50)
{
mSkinObject.push_back(rect);
rectangle(res,rect,Scalar(0,255,0));
++ iter;
}
else
{
iter = contours_set.erase(iter); //清除不满足条件的轮廓
}
}

Mat result(img.size(),CV_8UC1,Scalar(0));

Mat img_masked;  

drawContours(result,contours_set,-1,Scalar(255),CV_FILLED); //画轮廓  CV_FILLED

img0.copyTo(img_masked, result);  //将原图轮廓中的满足条件的轮廓内的图像拷贝到img_masked

imshow("提取",img_masked);

waitKey(10);

你可能感兴趣的:(轮廓筛选)