void findContours( InputArray image, OutputArrayOfArrays contours,
OutputArray hierarchy, int mode,
int method, Point offset = Point());
其中重要的参数为:
1.hierarchy:官方文档原文:
是一个类型为:vector
在枚举参数中选择:
CV_RETR_EXTERNAL:只检测最外围轮廓,包含在外围轮廓内的内围轮廓被忽略
CV_RETR_LIST: 检测所有的轮廓,包括内围、外围轮廓,但是检测到的轮廓不建立等级关系,彼此之间独立,没有等级关系,这就意味着这个检索模式下不存在父轮廓或内嵌轮廓,hierarchy向量内所有元素的第3、第4个分量都会被置为-1
CV_RETR_CCOMP: 检测所有的轮廓,但所有轮廓只建立两个等级关系,外围为顶层,若外围内的内围轮廓还包含了其他的轮廓信息,则内围内的所有轮廓均归属于顶层
CV_RETR_TREE: 检测所有轮廓,所有轮廓建立一个等级树结构。外层轮廓包含内层轮廓,内层轮廓还可以继续包含内嵌轮廓。
3.method:在枚举参数中取值:
CV_CHAIN_APPROX_NONE 保存物体边界上所有连续的轮廓点到contours向量内
CV_CHAIN_APPROX_SIMPLE 仅保存轮廓的拐点信息,把所有轮廓拐点处的点保存入contours向量内,拐点与拐点之间直线段上的信息点不予保留
CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
二、参考范例:----------------------------------------------------------- #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #includeusing namespace cv; using namespace std; int testContours(Mat thres_gray) { //调用findCounter函数 vector > contours; vector hierarchy; findContours(thres_gray, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); //绘制轮廓 Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3); int count1 = 0; int count2 = 0; 把所有轮廓都画出来 //------------------------------------------------------------------------------- for (size_t i = 0; i < contours.size(); i++) { count1 += 1; Scalar color = Scalar(0, 0, 255); drawContours(drawing, contours, (int)i, color, 1, LINE_AA, hierarchy, 0); } cout << "count1 is : n" << count1 << endl; //------------------------------------------------------------------------ 只画顶层轮廓 ------------------------------------------------------------------------------- //for (int index = 0; index >= 0; index = hierarchy[index][0]) { // count2 += 1; // Scalar color = Scalar(255, 0, 0); // drawContours(drawing, contours, (int)index, color, 1, LINE_AA, hierarchy, 0); //} //cout << "count2 is : n" << count2 << endl; ----------------------------------------------------------------------------------- imshow("Contours", drawing); waitKey(); return 0; }
参考:
1.Opencv Docs
2.Opencv轮廓检测findContours分析(层次结构) - 简书 (jianshu.com)
3. findContours函数参数详解_-牧野-的博客-CSDN博客_findcontours函数解析
4. OpenCV学习笔记十一-findcounters函数_安东time的博客-CSDN博客_findcounters函数



