OpenCV  3.4.0
Open Source Computer Vision
Contours : Getting Started

Goal

What are contours?

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.

How to draw the contours?

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))

Parameters
imagesource, 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.
contoursdetected contours.
hierarchycontaining information about the image topology. It has as many elements as the number of contours.
modecontour retrieval mode(see cv.RetrievalModes).
methodcontour approximation method(see cv.ContourApproximationModes).
offsetoptional 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))

Parameters
imagedestination image.
contoursall the input contours.
contourIdxparameter indicating a contour to draw. If it is negative, all the contours are drawn.
colorcolor of the contours.
thicknessthickness of lines the contours are drawn with. If it is negative, the contour interiors are drawn.
lineTypeline connectivity(see cv.LineTypes).
hierarchyoptional information about hierarchy. It is only needed if you want to draw only some of the contours(see maxLevel).
maxLevelmaximal 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.
offsetoptional contour shift parameter.

Try it

Contour Approximation Method

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.