轮廓提取过程中删除连通域面积小于某阈值的轮廓

#include#include #include #include #include usingnamespacestd;usingnamespacecv;intmain()

{

Mat srcImage;

Mat thresholdImage;

Mat grayImage;

srcImage = imread("1.png");

cvtColor(srcImage,grayImage,CV_BGR2GRAY);

threshold(grayImage, thresholdImage,0,255, CV_THRESH_OTSU+CV_THRESH_BINARY);

Mat resultImage;

thresholdImage.copyTo(resultImage);vector>contours;

findContours(resultImage,contours,CV_RETR_EXTERNAL,  CV_CHAIN_APPROX_NONE);vector>::iterator itc= contours.begin();while(itc!=contours.end())

{if( itc->size()<20)

{

itc= contours.erase(itc);

}else{

++itc;

}

}

drawContours(resultImage, contours, -1, Scalar(255), CV_FILLED);

imshow("原图",srcImage);

imshow("灰度",grayImage);

imshow("二值图",thresholdImage);

imshow("结果图",resultImage);

waitKey(0);return0;

}

你可能感兴趣的:(轮廓提取过程中删除连通域面积小于某阈值的轮廓)