How to find and draw Convex Hull of an image contour in OpenCV Python?

A Convex Hull is a convex curve that wraps around an object, similar to stretching a rubber band around the shape. Unlike contour approximation, a convex hull is always bulged outward or flat, never curved inward. It finds and corrects convexity defects in the original contour.

Syntax

To find the convex hull, we use the following function:

hull = cv2.convexHull(cnt, hull, clockwise, returnPoints)

Parameters

  • cnt ? The input contour points as an array

  • hull ? Output parameter, normally omitted

  • clockwise ? Orientation flag. If True, output is clockwise oriented

  • returnPoints ? Default is True. Returns coordinates when True, indices when False

Most commonly, we use the simplified version:

hull = cv2.convexHull(cnt)

To draw the convex hull, use:

cv2.drawContours(img, [hull], -1, (0,255,255), 3)

Step-by-Step Process

Follow these steps to find and draw convex hull:

  1. Import OpenCV library
  2. Read and convert image to grayscale
  3. Apply thresholding to create binary image
  4. Find contours using cv2.findContours()
  5. Calculate convex hull using cv2.convexHull()
  6. Draw the hull on the original image

Example 1: Single Contour Convex Hull

This example finds the convex hull for the first detected contour:

import cv2
import numpy as np

# Create a sample image with a star shape
img = np.zeros((300, 300, 3), dtype=np.uint8)
points = np.array([[150,50], [120,120], [50,120], [100,160], [80,230], 
                   [150,190], [220,230], [200,160], [250,120], [180,120]], np.int32)
cv2.fillPoly(img, [points], (255, 255, 255))

# 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 convex hull
hull = cv2.convexHull(cnt)

# Draw contour in blue and convex hull in yellow
img = cv2.drawContours(img, [cnt], 0, (255, 0, 0), 2)
img = cv2.drawContours(img, [hull], 0, (0, 255, 255), 3)

print("Original contour points:", len(cnt))
print("Convex hull points:", len(hull))

The output shows the difference between the original contour and its convex hull:

Number of contours detected: 1
Original contour points: 10
Convex hull points: 6

Example 2: Multiple Contours

This example processes multiple contours and draws their convex hulls:

import cv2
import numpy as np

# Create image with multiple shapes
img = np.zeros((400, 400, 3), dtype=np.uint8)

# Draw triangle
triangle = np.array([[100, 100], [150, 50], [200, 100]], np.int32)
cv2.fillPoly(img, [triangle], (255, 255, 255))

# Draw L-shape
l_shape = np.array([[250, 150], [300, 150], [300, 200], [350, 200], 
                    [350, 250], [250, 250]], np.int32)
cv2.fillPoly(img, [l_shape], (255, 255, 255))

# Draw star
star = np.array([[150, 200], [130, 250], [80, 250], [110, 280], 
                 [100, 330], [150, 300], [200, 330], [190, 280], 
                 [220, 250], [170, 250]], np.int32)
cv2.fillPoly(img, [star], (255, 255, 255))

# Convert to grayscale and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 100, 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 convex hull
    hull = cv2.convexHull(cnt)
    
    # Draw contour in green and hull in red
    img = cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
    img = cv2.drawContours(img, [hull], 0, (0, 0, 255), 3)
    
    print(f"Contour {i}: {len(cnt)} points ? Hull: {len(hull)} points")
Number of contours detected: 3
Contour 0: 3 points ? Hull: 3 points
Contour 1: 6 points ? Hull: 4 points
Contour 2: 10 points ? Hull: 6 points

Key Points

  • Convex hull eliminates concave portions of contours
  • The hull always has fewer or equal points compared to the original contour
  • Useful for shape analysis and object recognition
  • Green contours show original shape, red/yellow show convex hull

Conclusion

Convex hull is essential for shape analysis in computer vision. Use cv2.convexHull() to find the smallest convex boundary around any contour, which helps in object detection and geometric analysis tasks.

Updated on: 2026-03-26T22:06:35+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements