ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Contour Detection and Analysis in Python with OpenCV

author
Generated by
Krishna Adithya Gaddam

06/12/2024

Python

Sign in to read full article

Contour detection is a key aspect of image processing that allows us to identify shapes within images. Whether you are working with object detection, image segmentation, or any other task that requires understanding the boundaries of objects in your image, contours are incredibly useful. In this post, we will explore how to leverage OpenCV in Python for contour detection and analysis.

What are Contours?

Contours are curves that join all the continuous points along a boundary which have the same color or intensity. In simpler terms, contours can be visualized as isopoints in a topographical geography map—they outline the shape of an object.

Contours can help in various applications such as:

  • Shape analysis and recognition
  • Object detection
  • Image segmentation
  • Edge detection

Setting Up the Environment

Before we begin coding, ensure you have OpenCV installed in your Python environment. Use the following command to install it via pip:

pip install opencv-python

Also, to display images, you might find it useful to install matplotlib:

pip install matplotlib

Basic Steps for Contour Detection

To detect and analyze contours using OpenCV in Python, follow these steps:

  1. Read the image.
  2. Convert the image to grayscale.
  3. Apply Gaussian Blur to reduce noise and improve contour detection.
  4. Use Canny Edge Detection to find edges in the image.
  5. Find contours using the cv2.findContours() function.
  6. Draw contours using the cv2.drawContours() function.

Example Code

Let’s dive into the code with an example that shows the steps we outlined above:

import cv2 import numpy as np import matplotlib.pyplot as plt # Load the image image = cv2.imread('path/to/your/image.jpg') # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur blurred = cv2.GaussianBlur(gray, (5, 5), 0) # Use Canny edge detection edges = cv2.Canny(blurred, 75, 150) # Find contours contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Draw contours contour_image = np.zeros_like(image) cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 3) # Display the images plt.figure(figsize=(10, 5)) plt.subplot(121) plt.title('Edges') plt.imshow(edges, cmap='gray') plt.subplot(122) plt.title('Contours') plt.imshow(cv2.cvtColor(contour_image, cv2.COLOR_BGR2RGB)) plt.show()

Explanation of the Code

  • Reading the image: This is done using cv2.imread(). Make sure to provide the correct path to your image.
  • Grayscale Conversion: This step simplifies the data we need to process since color channels can complicate edge detection.
  • Gaussian Blur: Adding blur reduces noise which can lead to poor edge detection.
  • Canny Edge Detection: This algorithm detects a wide range of edges in images. The thresholds determine the sensitivity of edge detection.
  • Finding Contours: cv2.findContours() retrieves contours from the binary image (edges) created by the Canny edge detection.
  • Drawing Contours: We create a blank image and draw all the contours on it.

Analyzing Contours

Once we have detected contours, we can analyze them to derive various properties using functions like:

  • cv2.contourArea(contour): Calculates the area of a contour.
  • cv2.arcLength(contour, True): Computes the perimeter of the contour.
  • cv2.approxPolyDP(contour, epsilon, True): Approximates a polygonal curve with a specified precision.

Here’s an example to demonstrate how to analyze contours:

for contour in contours: # Calculate the area of each contour area = cv2.contourArea(contour) # Only consider significant contours if area > 100: # Calculate the perimeter perimeter = cv2.arcLength(contour, True) # Find corners of the contour epsilon = 0.02 * perimeter approx = cv2.approxPolyDP(contour, epsilon, True) print(f'Contour Area: {area}, Perimeter: {perimeter}') print(f'Number of corners: {len(approx)}')

In this snippet, we iterate through each contour, calculate its area, perimeter, and corners (if it’s a polygon). Based on specific area thresholds, you can filter out smaller contours that may not be relevant to your analysis.

Visualizing Contour Properties

To easily visualize the properties of each contour, you can add labels to your original image:

for idx, contour in enumerate(contours): if cv2.contourArea(contour) > 100: M = cv2.moments(contour) if M['m00'] != 0: cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) cv2.putText(image, f'Contour {idx}', (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (255, 0, 0), 2) # Display the image with contours labeled plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.title('Image with Contours and Labels') plt.show()

In this code, we calculate the centroid of each contour using moments and then label it directly on the original image.

Contour detection is powerful and forms the backbone of many applications in computer vision. Whether you are building a simple app to count objects or need a more complex operation for image recognition, understanding contours will significantly enhance your capability in manipulating images using OpenCV. By practicing with various images and tweaking parameters, you will develop a solid intuition for working with contours.

Popular tags

PythonOpenCVComputer Vision

Share now!

Like & bookmark

Related collections

  • Mastering NLP with spaCy

    22/11/2024 · Python

  • Django Mastery: From Basics to Advanced

    26/10/2024 · Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 · Python

  • Mastering NLTK for Natural Language Processing

    22/11/2024 · Python

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 · Python

Related articles

  • Enhancing Redis Security and Authentication in Python

    08/11/2024 · Python

  • Unlocking Motion Analysis

    06/12/2024 · Python

  • Advanced File Handling and Serialization Techniques in Python

    13/01/2025 · Python

  • Understanding Color Spaces and Transformations in Python

    06/12/2024 · Python

  • Understanding Python Decorators

    21/09/2024 · Python

  • Profiling and Optimizing Python Code

    13/01/2025 · Python

  • Stemming with Porter and Lancaster Stemmer in Python

    22/11/2024 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design