OpenCV
4.9.0
Open Source Computer Vision
|
Next Tutorial: Contour Features
Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.
To draw the contours, cv.drawContours function is used. It can also be used to draw any shape provided you have its boundary points.
We use the functions: cv.findContours (image, contours, hierarchy, mode, method, offset = new cv.Point(0, 0))
image | source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero pixels remain 0's, so the image is treated as binary. |
contours | detected contours. |
hierarchy | containing information about the image topology. It has as many elements as the number of contours. |
mode | contour retrieval mode(see cv.RetrievalModes). |
method | contour approximation method(see cv.ContourApproximationModes). |
offset | optional offset by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context. |
cv.drawContours (image, contours, contourIdx, color, thickness = 1, lineType = cv.LINE_8, hierarchy = new cv.Mat(), maxLevel = INT_MAX, offset = new cv.Point(0, 0))
image | destination image. |
contours | all the input contours. |
contourIdx | parameter indicating a contour to draw. If it is negative, all the contours are drawn. |
color | color of the contours. |
thickness | thickness of lines the contours are drawn with. If it is negative, the contour interiors are drawn. |
lineType | line connectivity(see cv.LineTypes). |
hierarchy | optional information about hierarchy. It is only needed if you want to draw only some of the contours(see maxLevel). |
maxLevel | maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours, all the nested-to-nested contours, and so on. This parameter is only taken into account when there is hierarchy available. |
offset | optional contour shift parameter. |
This is the fifth argument in cv.findContours function. What does it denote actually?
Above, we told that contours are the boundaries of a shape with same intensity. It stores the (x,y) coordinates of the boundary of a shape. But does it store all the coordinates ? That is specified by this contour approximation method.
If you pass cv.ContourApproximationModes.CHAIN_APPROX_NONE.value, all the boundary points are stored. But actually do we need all the points? For eg, you found the contour of a straight line. Do you need all the points on the line to represent that line? No, we need just two end points of that line. This is what cv.CHAIN_APPROX_SIMPLE does. It removes all redundant points and compresses the contour, thereby saving memory.