Edge detection vs. contour detection in computer vision

Computer vision is a fascinating field that enables machines to interpret and process visual information, allowing them to perform complex tasks like object detection, image recognition, and much more.

Two main techniques used in computer vision for feature extraction are edge detection and contour detection. In this Answer, we'll explore these concepts, their implementation, and their differences.

Feature extraction techniques
Feature extraction techniques

Feature extraction

Feature extraction is a process in computer vision that we use to simplify complex visual data, like images, into some basic patterns or features. The machines can recognize important information, helping in tasks like image recognition or object detection. Having understood the concept of feature extraction, let's move on to the comparison between edge and contour detection.

Different information is extracted from images
Different information is extracted from images

Edge detection

Simply put, edge detection is a technique used to identify the edges found in an image. We can define an edge to be a change in pixel or image intensity.

Numerous algorithms exist for edge detection. One of the most widely used ones is known as Canny edge detection, which is a multi-step edge detection technique that can detect edges by following these steps:

  1. Gaussian smoothing

  2. Calculating the gradient intensity

  3. Non-maximum suppression

  4. Double thresholding

  5. Edge tracking

Note: Other edge detection algorithms include Sobel, Fuzzy logic, etc.

Edge detection implementation

import cv2

def perform_canny_edge_detection(image_path, min_threshold, max_threshold):
    img = cv2.imread(image_path, 0)  
    edges = cv2.Canny(img, min_threshold, max_threshold)
    return edges

if __name__ == "__main__":
    input_image_path = 'sample.png'
    min_threshold = 50
    max_threshold = 150

    edges = perform_canny_edge_detection(input_image_path, min_threshold, max_threshold)

    cv2.imshow("Canny Edge Detection", edges)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
  1. In our implementation, we load the image in grayscale using cv2.imread(image_path, 0) so that edge detection can be carried out efficiently.

  2. We then apply the Canny edge detection algorithm using cv2.Canny() with the specified minimum and maximum thresholds.

  3. Finally, the edges detected in the image are displayed using cv2.imshow().

Edge detection demonstration

This is how the image appears inverted, and the edges are clearly defined.

Edge detection output
Edge detection output

We can view the original, grayscale, and converted images together in a plot using Matplotlib to add to the visual experience.

import cv2
import matplotlib.pyplot as plt

def perform_canny_edge_detection(image_path, min_threshold, max_threshold):
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, min_threshold, max_threshold)
    return img, gray, edges

if __name__ == "__main__":
    input_image_path = 'sample.png'
    min_threshold = 50
    max_threshold = 150

    original_img, grayscale_img, edges = perform_canny_edge_detection(input_image_path, min_threshold, max_threshold)

    plt.figure(figsize=(10, 6))

    plt.subplot(1, 3, 1)
    plt.imshow(cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB))
    plt.title('Original Image')

    plt.subplot(1, 3, 2)
    plt.imshow(grayscale_img, cmap='gray')
    plt.title('Grayscale Image')

    plt.subplot(1, 3, 3)
    plt.imshow(edges, cmap='gray')
    plt.title('Edge Detection')

    plt.tight_layout()
    plt.show()

Edge detection demonstration

We can now view all images together in one plot using Matplotlib.

Edge detection output
Edge detection output

Contour detection

Contour detection is another important technique used in computer vision that can identify and extract the boundaries of objects in an image. Contours are curves joining all continuous points along the boundary having the same color or intensity.

Contour detection implementation

import cv2

def perform_contour_detection(image_path):
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    return contours

if __name__ == "__main__":
    input_image_path = 'sample.png'

    contours = perform_contour_detection(input_image_path)

    img_with_contours = cv2.imread(input_image_path)
    cv2.drawContours(img_with_contours, contours, -1, (0, 255, 0), 2)

    cv2.imshow("Contour Detection", img_with_contours)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
  1. In our implementation, we first load the image and convert it to grayscale, just like we did for the edge detection implementation.

  2. We then apply binary thresholding using cv2.threshold() to create a binary image.

  3. The contours are found using cv2.findContours().

  4. Lastly, we draw them on the original image using cv2.drawContours().

Contour detection demonstration

This is how the image's boundaries are highlighted using continuous point lines.

Contour detection output
Contour detection output

We can view the original, grayscale, and converted images all together in a plot using Matplotlib to add to the visual experience.

import cv2
import matplotlib.pyplot as plt

def perform_contour_detection(image_path):
    img = cv2.imread(image_path)
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray_img, 127, 255, cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    return img, gray_img, contours

if __name__ == "__main__":
    input_image_path = 'sample.png'

    original_img, gray_img, contours = perform_contour_detection(input_image_path)

    img_with_contours = original_img.copy()
    cv2.drawContours(img_with_contours, contours, -1, (0, 255, 0), 2)

    plt.figure(figsize=(15, 5))

    plt.subplot(1, 3, 1)
    plt.imshow(cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB))
    plt.title('Original Image')
    plt.axis('on')

    plt.subplot(1, 3, 2)
    plt.imshow(gray_img, cmap='gray')
    plt.title('Grayscale Image')
    plt.axis('on')

    plt.subplot(1, 3, 3)
    plt.imshow(cv2.cvtColor(img_with_contours, cv2.COLOR_BGR2RGB))
    plt.title('Contour Detection')
    plt.axis('on')

    plt.tight_layout()
    plt.show()

Contour detection demonstration

We can now view all images together in one plot using Matplotlib.

Contour detection output
Contour detection output

Applications of these techniques

Object detection

We can efficiently detect objects both within images or videos, and we can use edge and contour detection for the feature detection needed in object detection.

Image segmentation

Contours can help segment an image into different meaningful regions. These regions can then be analyzed according to the specific need.

Robotics

We can employ edge detection in different obstacle-detection tasks. For instance, it can be useful in robots assisting the elderly or autonomous vehicles.

Medical image

A great use of contours is in detecting and analyzing objects in medical images, such as tumors or blood vessels.

Condensed differences table

Although both detection mechanisms ultimately find the boundaries of an image, the way they do it is different. Edge detection focuses on simply the drastic changes in brightness or pixels, while contour detection focuses on outlining closed objects, thus producing connected loops each time.

Below is a list of condensed differences between both of these techniques. Choosing which one to apply is a subjective matter and depends on the problem at hand.

Aspect

Edge detection

Contour detection

Robustness

Sensitive to noise and could produce false positives.

More robust to noise and provides accurate boundary representation.

Image type

Applied on grayscale images.

Applied on binary or grayscale images.

Processing

Doesn't usually require post-processing.

Contours may require additional post-processing (e.g., filtering, approximation).

Complexity

Generally simpler and faster to compute.

Can be more complex, especially with complex shapes.

Algorithm

Canny, Sobel, etc. OpenCV's cv. Canny can be used.

OpenCV's cv2.findContours() can be used.

Information

Provides information about intensity gradients.

Provides information about connected boundary points.

Overlapping

Edges may not be closed or connected.

Contours are always closed and connected loops.

Object Holes

Edges sometimes may not enclose object holes.

Contours will enclose object holes.

Test your knowledge!

Question

Why do we convert a colored image to a grayscale before the detection steps?

Show Answer

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved