OpenCV Python – How to find and draw extreme points of an object on an image?

Finding and drawing extreme points of objects in an image is useful for shape analysis and object detection. Extreme points are the leftmost, rightmost, topmost, and bottommost coordinates of a contour.

Algorithm Steps

To find and draw extreme points of objects, follow these steps:

  • Load and preprocess ? Read the input image using cv2.imread() and convert it to grayscale.

  • Apply thresholding ? Create a binary image using cv2.threshold() for better contour detection.

  • Find contours ? Detect object boundaries using cv2.findContours().

  • Calculate extreme points ? For each contour, find the leftmost, rightmost, topmost, and bottommost points.

  • Draw points ? Visualize extreme points using colored circles.

Finding Extreme Points Formula

The extreme points are calculated using NumPy's argmin() and argmax() functions:

leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

Example 1: Single Object

This example demonstrates finding extreme points for a single object ?

import cv2
import numpy as np

# Create a sample image with a simple shape
img = np.zeros((300, 400, 3), dtype=np.uint8)
cv2.rectangle(img, (100, 80), (300, 220), (255, 255, 255), -1)

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply thresholding
ret, thresh = cv2.threshold(gray, 150, 255, 0)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected:", len(contours))

# Select the first contour
cnt = contours[0]

# Find extreme points
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

print(f"Leftmost: {leftmost}")
print(f"Rightmost: {rightmost}")
print(f"Topmost: {topmost}")
print(f"Bottommost: {bottommost}")

# Draw extreme points
points = [leftmost, rightmost, topmost, bottommost]
colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0)]  # Red, Green, Blue, Yellow

for i, point in enumerate(points):
    cv2.circle(img, point, 8, colors[i], -1)

# Display result
cv2.imshow("Extreme Points", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Example 2: Multiple Objects

This example shows how to find extreme points for multiple objects in an image ?

import cv2
import numpy as np

# Create image with multiple shapes
img = np.zeros((400, 500, 3), dtype=np.uint8)
cv2.circle(img, (150, 150), 60, (255, 255, 255), -1)
cv2.rectangle(img, (300, 100), (450, 200), (255, 255, 255), -1)

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply thresholding
ret, thresh = cv2.threshold(gray, 150, 255, 0)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected:", len(contours))

# Process each contour
for i, cnt in enumerate(contours):
    # Find extreme points
    leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
    rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
    topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
    bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])
    
    points = [leftmost, rightmost, topmost, bottommost]
    
    # Draw extreme points for each object
    for point in points:
        cv2.circle(img, point, 6, (0, 0, 255), -1)
    
    print(f"Object {i+1} extreme points:")
    print(f"  Leftmost: {leftmost}, Rightmost: {rightmost}")
    print(f"  Topmost: {topmost}, Bottommost: {bottommost}")

# Display result
cv2.imshow("Multiple Objects Extreme Points", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Key Parameters

Function Parameter Purpose
cv2.threshold() Threshold value (150) Separates object from background
cv2.circle() Radius (4-8) Size of extreme point markers
argmin()/argmax() Axis (0=x, 1=y) Find minimum/maximum coordinates

Common Applications

  • Object orientation ? Determine object alignment and rotation

  • Bounding box calculation ? Create minimal enclosing rectangles

  • Shape analysis ? Measure object dimensions and proportions

  • Feature extraction ? Use extreme points as distinctive features

Conclusion

Extreme points provide valuable geometric information about objects in images. Use argmin() and argmax() on contour coordinates to find these points, then visualize them with cv2.circle() for analysis and debugging purposes.

Updated on: 2026-03-26T22:53:15+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements