OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Finding and drawing contours is fundamental in computer vision tasks, especially in object detection and shape analysis. Contours can be described as the boundaries of an object of interest in an image.
Here's a tutorial on how to find and draw contours using Python and OpenCV:
Setup:
First, you need to have OpenCV installed:
pip install opencv-python
Code to Find and Draw Contours:
import cv2 def find_and_draw_contours(image_path): # Read the image image = cv2.imread(image_path) # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply binary thresholding _, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # Find contours in the thresholded image # Note: In OpenCV 4.x, findContours returns only 2 arguments contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Draw the contours on the original image cv2.drawContours(image, contours, -1, (0, 255, 0), 2) # -1 means draw all contours # Display the original image with contours cv2.imshow('Contours', image) cv2.waitKey(0) cv2.destroyAllWindows() # Call the function find_and_draw_contours('path_to_your_image.jpg')
Run the Code:
After running the code, you should see a window displaying the original image with green contours around the detected objects.
The function cv2.findContours()
retrieves contours from the binary image. The cv2.RETR_EXTERNAL
flag retrieves only the external contours. If you wish to retrieve all the contours, including those inside the objects (like a contour inside 'O' or '8'), use cv2.RETR_LIST
.
cv2.CHAIN_APPROX_SIMPLE
compresses horizontal, diagonal, and vertical segments and leaves only their end points. If you want all the contour points without any compression, use cv2.CHAIN_APPROX_NONE
.
Thresholding is used here to create a binary image, but in a more complex scenario, you might need to use techniques like Canny edge detection, adaptive thresholding, or others.
cv2.drawContours()
is used to visualize the contours. You can customize the color, thickness, etc., to suit your needs.
This tutorial provides a straightforward method to find and draw contours in an image using OpenCV. The concept can be further expanded upon based on specific requirements, such as filtering contours by size, approximating contour shape, and so on.
Contour detection and visualization using OpenCV in Python:
import cv2 import numpy as np image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE) _, contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) result = cv2.drawContours(np.zeros_like(image), contours, -1, (255, 255, 255), 2) cv2.imshow('Contours', result) cv2.waitKey(0) cv2.destroyAllWindows()
Python OpenCV contour identification and drawing:
# Similar to the previous code snippet
Sample code for finding and drawing contours in OpenCV:
# Similar to the previous code snippet
Optimizing contour visualization for different image types in Python with OpenCV:
# Experiment with contour detection parameters for optimal results
Contour filtering and hierarchy in OpenCV for complex shapes:
# Use contour hierarchy and filtering to identify nested shapes
Python OpenCV contour drawing for object recognition:
# Implement object recognition using contour drawing techniques