Findcontours function, the prototype of this function is:
void Findcontours (Inputoutputarray image, Outputarrayofarrays contours, Outputarray hierar-
Chy, int mode, int method, point Offset=point ())
Parameter description
Input image must be a 2-value single-channel image
The contours parameter is an array of detected outlines, each of which is represented by a vector of point type
Hiararchy parameters and the same number of outlines, each contour contours[i] corresponds to 4 hierarchy elements hierarchy[I [0] ~hierarchy[I [3], respectively, representing the next contour, the previous outline, the parent contour, the index number of the inline outline If there is no corresponding entry, the value is set to a negative number.
Mode indicates the retrieval pattern of the contour
Cv_retr_external indicates that only the outer contour is detected
Cv_retr_list detected contour does not establish a hierarchy relationship
Cv_retr_ccomp establishes two levels of contour, the upper layer is the outer boundary, the inside layer is the inner hole boundary information. If there is also a connected object inside the hole, the object's boundary is also on the top layer.
Cv_retr_tree Create a hierarchical tree structure outline. Specific reference CONTOURS.C this demo
method as an approximate method of contour
Cv_chain_approx_none stores all the contour points, the pixel position difference of the adjacent two points is not more than 1, that is, Max (ABS (X1-X2), ABS (Y2-Y1)) ==1
Cv_chain_approx_simple compresses horizontal, vertical, diagonal elements, leaving only the end coordinates of that direction, such as a rectangular outline, with only 4 points to save contour information
Cv_chain_approx_tc89_l1,cv_chain_approx_tc89_kcos using TEH-CHINL CHAIN approximation algorithm
Offset represents an offset that represents the contour point, which can be set to any value. This parameter is useful for finding outlines in ROI images and for analyzing them throughout the image.
Findcontours will change the input 2 value image, so if you do not want to change the 2 value image, you need to create a new mat to store, findcontours after the outline information contours may be too complex and not smooth, The APPROXPOLYDP function can be used to approximate the polygonal curve.
The Contourarea function can get the size of the current contour containing area, and the filtering of the contour is convenient.
Findcontours are often used in conjunction with Drawcontours to draw outlines. The first parameter, image, represents the target image, the second parameter contours represents the input contour group, each set of outlines is composed of point vectors, the third parameter contouridx indicates the outline of the painting, if the parameter is negative, then draw all the outline, The fourth parameter is color of the contour, the fifth parameter thickness is the line width of the contour, if negative or cv_filled is the inside of the fill contour, the sixth parameter linetype is the Linetype, the seventh parameter is the contour structure information, the eighth parameter is Maxlevel
The complex contour is often not suitable for the detection of features, and here we introduce the extraction function of the convex envelope of a point set convexhull, the input parameter can be a contour in the Contours group, return the point set of the outer convex envelope
can also get the contour of the outsourcing of the envelope, using the function Boundingrect, if you want to rotate the outsourced envelope, using the function Minarearect, the return value is Rotatedrect, you can also get the contour of the outsourced envelope, The corresponding function is minenclosingcircle, the outer envelope ellipse of the contour is desired, the corresponding function is Fitellipse, the return value is also rotatedrect, and the corresponding ellipse can be drawn with the ellipse function.
If you want to obtain the polygon's multi-order moment according to the contour information of the polygon, you can use the class moments, which can get all the moments within the 3 order of the polygon and the grating shape, there are variable m00,m10,m01,m20,m11,m02,m30,m21,m12,m03 in the class. For example, the centroid of a polygon is x = M10/m00,y = m01/m00.
If you want to get a bit of information about a polygon enclosing outline, you can call the Pointpolygontest function, which returns a value that is the distance from the nearest boundary of the contour of the point, with positive values inside the contour, negative values outside the contour, and 0 representing the boundary.
[Reprint]findcontours function parameter description and related function