DEV Community

Cover image for How to detect edges in an image using opencv in Python
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

How to detect edges in an image using opencv in Python

what is edge detection?

Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness. 

where edge detection is used?

Edge detection is used for image segmentation and data extraction in areas such as image processingcomputer vision, and* machine vision*, so knowing how to do it will eventually pay you off.

Requirements

There are several edge detection algorithms and different libraries supporting it but in this tutorial, I'm going to show you how to do it using OpenCV using the Canny algorithm.

Installation

For window just use normal


 while for Linux & Mac use

 ```pip3```

 to install the dependencies  just as shown below;



```bash
pip install opencv-python
pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

Let's get started

Once we have installed now we ready to go to detecting edges with python using Canny algorithms.

we are going to use the OpenCV method imread() to load an image from the file, use Canny() *to detect the edges, and then finally visualizing the images before detection and after using Matplotlib*

Reading images with OpenCV

To read an image from a file using the imread() method you need to provide two-parameter, one is for the path to our raw image, and the next one is a mode of reading which can either by GRAY, RGB, HSV, HSL, and etc.

OpenCV syntax to read image

import cv2

image = cv2.imread(path_to_image, mode_of_reading)
Enter fullscreen mode Exit fullscreen mode

Canny algorithms usually work well when the image is in grayscale, there is a shortcut in OpenCV to open an image in a grayscale mode which is done by putting 0 on the mode of reading.

Using Canny algorithms to detect the edges

To detect edges with Canny you have to specify your raw image, lower pixel threshold, and higher pixel threshold in the order shown below;

image_with_edges = cv2.Canny(raw_image, l_threshold, h_theshold)
Enter fullscreen mode Exit fullscreen mode

How threshold affect edge detection?

The intensity gradient of a pixel is greater than the higher threshold, it will be added as an edge pixel in the output image otherwise it will be rejected completely.

Finalizing our code and Visualizing it with Matplotlib

Now that's we learned the basics of OpenCV together with Canny detection algorithms now let's put them together to sample real-world application

Let's detect the edges in the below sample case image road.jpg

Alt Text

Using the knowledge we just learned above, I have bundled everything together to come up with below final code below, when you run it, it will load an image, perform detection and display it using Matplotlib.

import cv2
import matplotlib.pyplot as plt 

def detect_edge(image):
    ''' function Detecting Edges '''

    image_with_edges = cv2.Canny(image , 100, 200)

    images = [image , image_with_edges]

    location = [121, 122]

    for loc, img in zip(location, images):
        plt.subplot(loc)
        plt.imshow(img, cmap='gray')

    plt.savefig('edge.png')
    plt.show()

image = cv2.imread('road.jpg', 0)
detect_edge(image)
Enter fullscreen mode Exit fullscreen mode

Ouput

Once executed it will render a picture with edges just as shown in the picture below;

🎉🎉🎉 Congratulations you have just learned how to detect edges in python, you should be proud of yourself, Tweet now to share this good news with your fellow developers.

Also in case of anything, drop it in the comment box below and I will get back to you ASAP

GitHub logo Kalebu / Edge-Detection

A python script for image detection based on canny algorithms

Edge-Detection

Guide to Performing Edge Detection in Python using Opencv built-in Algorithms Canny()




Top comments (0)