OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Extracting the coordinates of contours can be valuable in many computer vision applications, such as object recognition, shape analysis, and feature extraction. In this tutorial, we will walk through how to find and print the coordinates of contours in an image using Python and OpenCV.
Setup:
Ensure OpenCV is installed:
pip install opencv-python
Code to Find and Print Coordinates of Contours:
import cv2 def find_contour_coordinates(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 contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Loop over each contour for idx, contour in enumerate(contours): print(f"Coordinates of contour {idx + 1}:") # Loop over each point in the contour for point in contour: x, y = point[0] print(f"(x={x}, y={y})") print("-" * 30) # Display the image with contours (for visualization) cv2.drawContours(image, contours, -1, (0, 255, 0), 2) cv2.imshow('Contours', image) cv2.waitKey(0) cv2.destroyAllWindows() # Call the function find_contour_coordinates('path_to_your_image.jpg')
Run the Code:
After running the code, you'll see the coordinates of each contour printed in the console. The image will also be displayed with the contours highlighted for visualization.
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
.
The cv2.CHAIN_APPROX_SIMPLE
flag compresses horizontal, diagonal, and vertical segments of the contour and leaves only their end points. If you want all the contour points without any compression, use cv2.CHAIN_APPROX_NONE
.
This tutorial provides a basic method to find and print the coordinates of contours in an image using OpenCV. The concept can be further extended based on specific requirements, such as extracting the contour's bounding box or computing the contour's centroid.
Extracting coordinates of contours 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) for contour in contours: for point in contour: x, y = point[0] print(f'Coordinate: ({x}, {y})')
Python OpenCV contour analysis for coordinate extraction:
# Similar to the previous code snippet
Sample code for obtaining contour coordinates in OpenCV:
# Similar to the previous code snippet
Optimizing contour coordinate extraction in Python with OpenCV:
# Experiment with contour analysis parameters for better coordinate extraction
Python OpenCV contour coordinates vs bounding box:
# Compare contour coordinate extraction with bounding box methods
Contour filtering based on coordinates and geometry in OpenCV:
# Filter contours based on coordinates and geometry criteria
Python OpenCV contour coordinate visualization and manipulation:
# Visualize and manipulate contour coordinates for specific applications