Contours – Basic Detection and Plotting
A contour can only be detected in a binary image using OpenCV. To detect contours in colored (BGR) or grayscale images, you will first need to convert them to binary.
Note
In OpenCV, a colored image has its channels in the order of BGR (blue
, green
, and red
) instead of RGB.
To find the contours in a colored image, first, you will need to convert it to grayscale. After that, you will segment it to convert it to binary. This segmentation can be done either with thresholding based on a fixed grayscale value of your choice or by using Otsu's method (or any other method that suits your data). Following is a flowchart of contour detection:
The command to detect contours in an image is as follows:
contours, hierarchy = cv2.findContours(source_image, \ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...