此前在《【OpenCV3】图像轮廓查找与绘制——cv::findContours()与cv::drawContours()详解》一文中,详细介绍了图像轮廓的检测与绘制,但是在实际的应用中,往往需要检测目标的最大轮廓,但是OpenCV本身并没有封装这样一个函数,下面就贴上封装好的接口,供参考使用。
说明:对于最大轮廓的定义,有些以轮廓的点数最多为标准,有的以所包围的面积最大为标准,这里将两种都实现一下。
1、C接口的封装(最多点数)
CvSeq* cvFindBiggestCountour(IplImage *binaryImage)
{
int polyHull0 = 1;
CvPoint offset;
offset.x = 0;
offset.y = 0;
CvMemStorage *tempStorage = cvCreateMemStorage(0);
CvSeq *contour;
CvSeq *c;
int nContours = 0;
double largest_length = 0;
double len = 0;
CvContourScanner scanner;
CvSlice slice = CV_WHOLE_SEQ;
scanner = cvStartFindContours(binaryImage, tempStorage, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset);
while ((c = cvFindNextContour(scanner)) != 0)
{
len = cvContourPerimeter(c);
if (len > largest_length)
{
largest_length = len;
}
}
scanner = cvStartFindContours(binaryImage, tempStorage, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset);
while ((c = cvFindNextContour(scanner)) != 0)
{
len = cvContourPerimeter(c);
double q = largest_length;
if (len < q)
cvSubstituteContour(scanner, 0);
else
{
CvSeq *newC;
if (polyHull0)
newC = cvApproxPoly(c, sizeof(CvContour), tempStorage, CV_POLY_APPROX_DP, 2, 0);
else
newC = cvConvexHull2(c, tempStorage, CV_CLOCKWISE, 1);
cvSubstituteContour(scanner, newC);
}
nContours++;
}
printf("End find contours!\n");
contour = cvEndFindContours(&scanner);
cvReleaseMemStorage(&tempStorage);
cvRelease(&c);
return contour;
}
2、C接口的封装(最大面积)
CvSeq* cvFindBiggestCountour(IplImage *binaryImage)
{
int polyHull0 = 1;
CvPoint offset;
offset.x = 0;
offset.y = 0;
CvMemStorage *tempStorage = cvCreateMemStorage(0);
CvSeq *contour;
CvSeq *c;
int nContours = 0;
double largest_length = 0;
double len = 0;
CvContourScanner scanner;
CvSlice slice = CV_WHOLE_SEQ;
scanner = cvStartFindContours(binaryImage, tempStorage, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset);
while ((c = cvFindNextContour(scanner)) != 0)
{
len = cvContourArea(c, slice, 0);
if (len > largest_length)
{
largest_length = len;
}
}
scanner = cvStartFindContours(binaryImage, tempStorage, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset);
while ((c = cvFindNextContour(scanner)) != 0)
{
len = cvContourArea(c, slice, 0);
double q = largest_length;
if (len < q)
cvSubstituteContour(scanner, 0);
else
{
CvSeq *newC;
if (polyHull0)
newC = cvApproxPoly(c, sizeof(CvContour), tempStorage, CV_POLY_APPROX_DP, 2, 0);
else
newC = cvConvexHull2(c, tempStorage, CV_CLOCKWISE, 1);
cvSubstituteContour(scanner, newC);
}
nContours++;
}
printf("End find contours!\n");
contour = cvEndFindContours(&scanner);
cvReleaseMemStorage(&tempStorage);
cvRelease(&c);
return contour;
}
3、C++接口封装(最多点数)
std::vector<cv::Point> findBiggestContour(cv::Mat binary_image)
{
std::vector<std::vector<cv::Point>> contours;
int largest_area = 0;
int largest_contour_index = 0;
cv::findContours(binary_image, contours, cv::noArray(), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++)
{
int a = contours[i].size();
if (a > largest_area){
largest_area = a;
largest_contour_index = i;
}
}
return contours[largest_contour_index];
}
4、C++接口封装(最大面积)
std::vector<cv::Point> findBiggestContour(cv::Mat binary_image)
{
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
int largest_area = 0;
int largest_contour_index = 0;
cv::findContours(binary_image, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) // iterate through each contour.
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a > largest_area){
largest_area = a;
largest_contour_index = i; //Store the index of largest contour
}
}
return contours[largest_contour_index];
}
其中C++接口的测试代码如下
cv::Mat src = cv::imread("Hepburn.png", 1);
cv::Mat result = src.clone();
cv::Mat gray;
cv::cvtColor(src, gray, CV_BGR2GRAY);
cv::Mat binary;
cv::threshold(gray, binary, 100, 255, cv::THRESH_BINARY);
std::vector< std::vector< cv::Point> > contours;
std::vector< cv::Point> biggest_contour;
std::vector< std::vector< cv::Point> > temp_contours;
cv::findContours(
binary,
contours,
cv::noArray(),
cv::RETR_LIST,
cv::CHAIN_APPROX_SIMPLE
);
biggest_contour = findBiggestContour(binary);
temp_contours.push_back(biggest_contour);
cv::drawContours(result, contours, -1, cv::Scalar(0, 255, 0));
cv::drawContours(result, temp_contours, 0, cv::Scalar(0, 0, 255));
cv::imshow("src", src);
cv::imshow("gray", gray);
cv::imshow("binary", binary);
cv::imshow("result", result);
cv::waitKey(0);
return 0;
测试结果如下,其中红色曲线代表的是最大轮廓:
参考:https://harismoonamkunnu.blogspot.tw/2012/11/opencv-find-biggest-contour.html