删去小于阈值的连通区域

来源:互联网 发布:留学中介机构排名知乎 编辑:程序博客网 时间:2024/05/22 08:18

程序:

//std::vector<std::vector<cv::Point>> contours;
//vector<Vec4i> hierarchy;
//cv::findContours(image_gray, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);// CV_RETR_EXTERNAL CV_RETR_CCOMP
//cv::Mat result(image_gray.size(), CV_8U, cv::Scalar(0)); 
//int index;
//double area, maxArea(0);
//for (int i = 0; i < contours.size(); i++)
//{
// area = contourArea(Mat(contours[i]));
// if (area > maxArea)
// {
// maxArea = area;
// index = i;
// }
//}
//drawContours(result, contours, index, Scalar(255), 2);
//std::cout << " Area " << area << std::endl;
//cv::namedWindow("Contours", 0);
//cv::imshow("Contours", result);
//std::cout << "result.cols " << result.cols << std::endl;
//std::cout << "result.rows " << result.rows << std::endl;

下面是bwareaopen(image_gray,10000); 源代码:

void bwareaopen(Mat& im, double size)
{
// Only accept CV_8UC1
if (im.channels() != 1 || im.type() != CV_8U)
return;


// Find all contours
vector<vector<Point> > contours;
findContours(im.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);


for (int i = 0; i < contours.size(); i++)
{
// Calculate contour area
double area = contourArea(contours[i]);


// Remove small objects by drawing the contour with black color
if (area > 0 && area <= size)
drawContours(im, contours, i, CV_RGB(0,0,0), -1);
}
}

0 0
原创粉丝点击