A contour is a curve joining all the continuous points along the boundary with the same color value. The detecting of contours in an image is very useful if you want to detect the boundaries in images. In an image, the edges are computed as points that are the extremes of the image gradient in the direction of the gradient. Contours are often obtained from edges, but they are aimed to be object contours. Thus, they need to be closed curves and are different from edges.
It is helpful to threshold an image before extracting contours to increase the accuracy of the image.
OpenCV has cv2.findContours()
to find the contours in an image. It takes an image, a contour retrieval mode, and a contour approximation method as arguments and returns the contours of the image. Contour retrieval mode can be CV_RETR_EXTERNAL
, CV_RETR_LIST
, CV_RETR_CCOMP
, or CV_RETR_TREE
. The contour approximation method can be CV_CHAIN_APPROX_NONE
, CV_CHAIN_APPROX_SIMPLE
, CV_CHAIN_APPROX_TC89_L1
, or CV_CHAIN_APPROX_TC89_KCOS...