Contours

Contours are curves joining point along a boundary having same colour or intensity. To accurately use the contour features, use a binary image. Since the findContours function modifies the original image always backup the original image in a separate variable. Also the object to be detected should white on a black background.

Once the image binarization is done, Contour detection can be carried out.
The findContours() function uses 3 arguments: First is the image, second the contour retrieval mode, third is contour approximation method. The outputs are the image,contours and hierarchy. Contours is a list of all the contours in the image. Each contour is an array of the coordinates making the contour.

To draw the contours we use the drawContours function. It takes as argument: the image name, the Python list containing all the contours generated by findContours, the index of the contour to draw and the colour and thickness of the boundary.

The contour approximation method is the third parameter in the findContours function. It can be set to cv2.CHAIN_APPROX_NONE which detects and saves all the boundary points in the contour or cv2.CHAIN_APPROX_SIMPLE which only saves the end points of the contour. The latter removes redundancy and also saves memory.

Advertisements