您的位置:首页 > 其它

删去小于阈值的连通区域

2016-05-14 11:58 363 查看
程序:

//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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: