[OpenCV Tutorial] Contour Detection Process


1. Find contours

1.1 API

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

/** @overload */
CV_EXPORTS void findContours( InputArray image, OutputArrayOfArrays contours,
                              int mode, int method, Point offset = Point());
  • The parameters are as follows
parameter meaning
image Input picture, data type Mat
contours Save the point coordinates of the output contour. Usually, the vector<vector<Point>> data type is used, and it can be seen from Point that the coordinates are stored.
hierarchy Optional parameter, save the hierarchical relationship of the output contour. Usually the vector<Vec4i> data type is used. See details below
mode Contour level detection mode, see below for details
method How to store contour coordinate points, see below for details
offset Additional offset, added to each detected contour point, can be negative. When the analyzed image is the ROI of another image, by adding or subtracting this offset, the detection results of the ROI image can be projected to the corresponding position of the original image.
  • hierarchy[i][0]: The index number of the next contour at the same level of the i-th contour.
  • hierarchy[i][1]: The index number of the previous contour of the same level of the i-th contour.
  • hierarchy[i][2]: The index number of the sub-contour of the i-th contour.
  • hierarchy[i][3]: The index number of the parent contour of the i-th contour.
  • If the current contour does not correspond to the next 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.

1.2 Contour level detection mode: index number (level)

enum RetrievalModes {
    
    
    RETR_EXTERNAL  = 0,
    RETR_LIST      = 1,
    RETR_CCOMP     = 2,
    RETR_TREE      = 3,
};

RETR_EXTERNAL (index order: bottom right to top left)

Only the outermost contour is detected, and inner contours contained within the outer contour are ignored.
Insert image description here

RETR_LIST(recommended) (index order: from bottom right to top left, from outside to inside)

Detect all contours, including inner and outer contours, but the detected contours do not establish a hierarchical relationship, which means that there is no parent contour or embedded contour in this retrieval mode, so the hierarch[i] vector of all elements 3. The fourth component will be set to -1.

RETR_CCOMP(not recommended) (index order: from inside to outside, from bottom right to top left)

All contours are detected, but only two hierarchical relationships are established for all contours. The outer periphery is the top layer. If the inner contour within the outer periphery also contains other contour information, all contours within the inner periphery belong to the top layer.

Insert image description here

RETR_TREE(recommended)

All contours are detected and a hierarchical tree structure is established for all contours. The outer contour contains the inner contour, and the inner contour can continue to contain inline contours.

Insert image description here

1.3 Contour coordinate point storage method

enum ContourApproximationModes {
    CHAIN_APPROX_NONE      = 1,
    CHAIN_APPROX_SIMPLE    = 2,
    CHAIN_APPROX_TC89_L1   = 3,
    CHAIN_APPROX_TC89_KCOS = 4
};
  • Optional values ​​for method are as follows
method optional value meaning
CHAIN_APPROX_NONE Save all continuous contour points on the object boundary into the contours vector
CHAIN_APPROX_SIMPLE(recommended) Only the inflection point information of the contour is saved, and all points at the inflection points of the contour are saved into the contours vector. The information points on the straight line segments between the inflection points are not retained, which is relatively efficient.
CHAIN_APPROX_TC89_L1或CV_CHAIN_APPROX_TC89_KCOS Use tehChinl chain approximation algorithm (not important)

2. Draw the outline

2.1 API

CV_EXPORTS_W void drawContours( InputOutputArray image, InputArrayOfArrays contours,
                              int contourIdx, const Scalar& color,
                              int thickness = 1, int lineType = LINE_8,
                              InputArray hierarchy = noArray(),
                              int maxLevel = INT_MAX, Point offset = Point() );
  • The parameters are as follows
parameter meaning
image Canvas for drawing outlines, data type Mat
contours Input outline, data type vector<vector<Point>>
contourIdx(contour index) The index value of the contour to be drawn, enter -1 to draw all contours
color The color of the drawn line
thickness The thickness of the drawn line. If it takes a negative value, it means filling
lineType The connectivity type for drawing lines
hierarchy Optional hierarchy information. It is only used when you need to draw some outlines. (See parameter maxLevel for details) The default is noArray(), which returns an empty array.
maxLevel The highest level for drawing contour lines. This parameter is only considered when the parameter hierarchy is valid. See the table below for details
offset Extra offset, which is added to each drawn contour point, can be negative. When the analyzed image is the ROI of another image, by adding or subtracting this offset, the rendering result of the ROI image can be projected to the corresponding position of the original image.
  • Optional values ​​for maxLevel are as follows:
maxLevel optional value meaning
0 Only the specified outline is drawn
1 Draw the specified contour and its subordinate contours
2 Draws the specified contour and all its sub-contours

3.Contour area and perimeter

3.1 Area (non-in-place algorithm)

CV_EXPORTS_W double contourArea( InputArray contour, bool oriented = false );
  • The parameters are as follows
parameter meaning
contour A certain outline , data type vector<Point>
oriented Directional area signs (not important). If true: This function returns the value of a marked area depending on the direction of the contour (clockwise or counterclockwise). If false: The default value means that the absolute value without direction is returned.
  • This function uses Green's formula to calculate the area of ​​a contour. For contours with self-intersection points, this function will almost certainly give incorrect results.

3.2 Perimeter (non-in-place algorithm)

CV_EXPORTS_W double arcLength( InputArray curve, bool closed );
  • The parameters are as follows
parameter meaning
curve A certain outline , data type vector<Point>
closed Whether the outline is closed

4.Polygon approximation

CV_EXPORTS_W void approxPolyDP( InputArray curve,
                                OutputArray approxCurve,
                                double epsilon, bool closed );
  • The parameters are as follows
parameter meaning
curve A certain outline , data type vector<Point>
approxCurve Output polygon point set, data type vector<Point>
epsilon Set the accuracy. The smaller it is, the higher the accuracy is. The polygon is closer to the curve, and the fitting effect is better but the efficiency is low.
closed Whether the outline is closed

5. Convex hull

CV_EXPORTS_W void convexHull( InputArray points, OutputArray hull,
                              bool clockwise = false, bool returnPoints = true );
  • The parameters are as follows
parameter meaning
points Input point set, data type vector<Point>
hull Output the convex hull. Data type depends on returnPoints, vector<Point> or vector<int>
clockwise The direction of rotation of the line fitting the convex hull, clockwise if TRUE, counterclockwise otherwise.
returnPoints If true, the coordinates of the point are stored in hull. If it is false, the index of the point is stored in the hull, and the index value is obtained according to the parameter points. Default is true

6. Encircling rectangle

6.1 Minimum circumscribed rectangle (return RotatedRect)

CV_EXPORTS_W RotatedRect minAreaRect( InputArray points );
  • The parameters are as follows
parameter meaning
points Input point set, data type vector<Point>

6.2 Maximum outer rectangle (return Rect)

CV_EXPORTS_W Rect boundingRect( InputArray array );
  • The parameters are as follows
parameter meaning
points Input point set, data type vector<Point>

Guess you like

Origin blog.csdn.net/qq_50791664/article/details/129525278