How to Perform Edge Detection in Python using OpenCV

Abdou Rockikz · 10 oct 2019

Abdou Rockikz · 3 min read · Updated nov 2019 · Machine Learning · Computer Vision

Edge detection is an image processing technique for finding the boundaries of objects within images. It mainly works by detecting discontinuities in brightness. One of the most popular and widely used algorithm is Canny edge detector.

Canny edge detector is an edge detection operator that uses multi-stage algorithm to detect a wide range of edges in images.

The main stages are:

  1. Filtering out noise using Gaussian blur algorithm.
  2. Finding the strength and direction of edges using Sobel Filters.
  3. Isolating the strongest edges and thin them to one-pixel wide lines by applying non-maximum suppression.
  4. Using hysteresis to isolate the best edges.

Learn more here about the theory behind Canny edge detector.

Alright, let's implement it in Python using OpenCV, first install it:

pip3 install opencv-python matplotlib numpy

Open up a new Python file and follow along:

import cv2
import numpy as np
import matplotlib.pyplot as plt

Now let's read the image when want to detect its edges:

# read the image
image = cv2.imread("little_flower.jpg")

I have an example image in my current directory, make sure you do too.

Before we pass the image to the Canny edge detector, we need to convert the image to gray scale:

# convert it to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Let's see it:

# show the grayscale image
plt.imshow(gray, cmap="gray")
plt.show()

Image in Gray Scale

All we need to do now, is to pass this image to cv2.Canny() function which finds edges in the input image and marks them in the output map edges using the Canny algorithm:

# perform the canny edge detector to detect image edges
edges = cv2.Canny(gray, threshold1=30, threshold2=100)

The smallest value between threshold1 and threshold2 is used for edge linking. The largest value is used to find initial segments of strong edges.

Let's see the resulting image:

Canny Edge Detection Resulting Image

Interesting, try to fine tune the threshold values and see if you can make it better.

If you want to use the live camera, here is the full code for that:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 30, 100)
    cv2.imshow("edges", edges)
    cv2.imshow("gray", gray)
    if cv2.waitKey(1) == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

Alright, we are done !

The purpose of detecting edges is to capture important events and changes in properties of the world. It is one of the fundamental steps in image processing, image pattern recognition and computer vision techniques.

Here is the next tutorial: How to Detect Shapes in Images in Python.

Happy Learning ♥

View Full Code
Sharing is caring!


Read Also





Comment panel

   
Comment system is still in Beta, if you find any bug, please consider contacting us here.