OpenCV findContours detailed explanation and examples

1. Function prototype and parameters:

void findContours( InputArray image, OutputArrayOfArrays contours,
                              OutputArray hierarchy, int mode,
                              int method, Point offset = Point());

 The important parameters are:

1.hierarchy:

Original official document:

is a variable of type: vector<Vec4i> . Vec4i is an alias of Vec<int,4>, which defines a vector that "each element in the vector contains 4 int variables". Each element in the vector holds an array of 4 int integers. The elements in the vector hiararchy correspond to the elements in the contour vector contours, and the vectors have the same capacity. The 4 int variables of each element in the hierarchy vector——hierarchy[i][0] ~hierarchy[i][3], which respectively represent the next contour, the previous contour, the parent contour, and the embedded contour of the i-th contour The index number of the contour. If the current contour has no corresponding subsequent contour, previous contour, parent contour or embedded contour, the corresponding bits of hierarchy[i][0] ~hierarchy[i][3] are set to the default value -1.

2.mod:

Choose among the enumerated parameters:

CV_RETR_EXTERNAL : Only the outermost contour is detected, and the inner contour contained in the outer contour is ignored

CV_RETR_LIST:   Detect all contours, including inner and outer contours, but the detected contours do not establish a hierarchical relationship, are independent of each other, and have no hierarchical relationship, which means that there is no parent contour or embedded contour in this retrieval mode. The third and fourth components of all elements in the hierarchy vector will be set to -1

CV_RETR_CCOMP:  Detect all contours, but all contours only establish two hierarchical relationships. The outer periphery is the top layer. If the inner contours in the outer periphery contain other contour information, all contours in the inner periphery belong to the top layer

CV_RETR_TREE: Detect all contours, and build a hierarchical tree structure for all contours. The outer contour contains the inner contour, and the inner contour can continue to contain the inner contour.

3.method:

Take the value in the enumeration parameter:

CV_CHAIN_APPROX_NONE saves all continuous contour points on the object boundary into the contours vector

CV_CHAIN_APPROX_SIMPLE only saves the inflection point information of the contour, saves all points at the inflection point of the contour into the contours vector, and does not retain the information points on the straight line segment between the inflection point and the inflection point

 CV_CHAIN_APPROX_TC89_L1, CV_CHAIN_APPROX_TC89_KCOS use teh-Chinl chain approximation algorithm

2. Reference example:

-----------------------------------------------------------

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int testContours(Mat thres_gray)
{
	//调用findCounter函数
	vector<vector<Point> > contours;
	vector<Vec4i> 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;

}

reference:

1.Opencv Docs

2. Opencv contour detection findContours analysis (hierarchical structure) - Short Book (jianshu.com)

3.  Detailed explanation of findContours function parameters

4.  OpenCV study notes eleven - findcounters function_Anton time's blog-CSDN blog_findcounters function 

Guess you like

Origin blog.csdn.net/jerry_yu_1/article/details/124733760