Open In App

Find Co-ordinates of Contours using OpenCV | Python

Last Updated : 11 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In computer vision, contours outline object shapes by connecting boundary points of the same intensity or color. With OpenCV’s cv2.findContours(), we can extract these contours as NumPy arrays to get precise (x, y) coordinates, which can be displayed on images or used for analysis like area, perimeter and shape properties.

Approach

  1. Read the image in both color and grayscale formats.
  2. Convert the grayscale image to binary using thresholding.
  3. Detect contours with cv2.findContours().
  4. Approximate the contours using cv2.approxPolyDP() to simplify the shape.
  5. Draw contours on the original image for visualization.
  6. Extract coordinates of all vertices by flattening the contour array.
  7. Label the coordinates on the image using cv2.putText().

Note: The first coordinate usually corresponds to the topmost vertex, useful for orientation tasks (like finding the tip of an arrow).

Python Implementation

Python
import numpy as np
import cv2

font = cv2.FONT_HERSHEY_COMPLEX

# Load image
img2 = cv2.imread('test.jpg', cv2.IMREAD_COLOR)
img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)

# Binarize image
_, threshold = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY)

# Find contours
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    # Approximate and draw contour
    approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
    cv2.drawContours(img2, [approx], 0, (0, 0, 255), 5)

    # Flatten points
    n = approx.ravel()
    i = 0
    for j in n:
        if i % 2 == 0:  # x, y coords
            x, y = n[i], n[i + 1]
            coord = f"{x} {y}"
            if i == 0:  # first point
                cv2.putText(img2, "Arrow tip", (x, y), font, 0.5, (255, 0, 0))
            else:
                cv2.putText(img2, coord, (x, y), font, 0.5, (0, 255, 0))
        i += 1

# Show result
cv2.imshow('Contours with Coordinates', img2)

# Exit on 'q'
if cv2.waitKey(0) & 0xFF == ord('q'):
    cv2.destroyAllWindows()

Input image:

Output: