一、介绍
findContours() :The function retrieves contours from the binary image。
二、代码
void main()
{
Mat src = imread("match00.bmp", IMREAD_GRAYSCALE);
Mat mask;
threshold(src, mask, 128, 255, cv::THRESH_BINARY_INV);
Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::morphologyEx(mask, mask, cv::MORPH_CLOSE, element, cv::Point(-1, -1), 1);
vector<vector<Point>> conts;
cv::findContours(mask, conts, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
cout << "RETR_LIST = " << conts.size() << endl;
for (int i = 0; i < conts.size(); i++)
{
double len = cv::arcLength(conts[i], false);
double area = cv::contourArea(conts[i]);
cout << "len = " << len << ", area = " << area << endl;
}
}
三、说明
1、原图
2、闭运算后的二值图像
3、RETR_EXTERNAL模式下计算的轮廓个数(只提取外轮廓)
4、RETR_LIST模式下计算的轮廓个数(提取内轮廓和外轮廓)