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.
Illustration:
When we join all the points on the boundary of an object, we get a contour. Typically, a specific contour refers to boundary pixels that have the same color and intensity. OpenCV makes it really easy to find and draw contours in images. It provides two simple functions:
findContours()
drawContours()
Also, it has two different algorithms for contour detection:
CHAIN_APPROX_SIMPLE
CHAIN_APPROX_NONE
OpenCV makes this a fairly simple task. Just follow these steps:
-
Read the Image and convert it to Grayscale Format
Read the image and convert the image to grayscale format. Converting the image to grayscale is very important as it prepares the image for the next step. Converting the image to a single channel grayscale image is important for thresholding, which in turn is necessary for the contour detection algorithm to work properly.
-
Apply Cannny Edge Detection
While finding contours, first always apply binary thresholding or Canny edge detection to the grayscale image. Here, we will apply Canny Edge Detection.
-
Find the Contours
Use the findContours() function to detect the contours in the image.
-
Draw Contours on the Original RGB Image
Once contours have been identified, use the drawContours() function to overlay the contours on the original RGB image.
CONTOURS | |
---|---|
Gray | Edges |
Contours | |