Simple Edge Detection Model using Python

How machines uses edge detection to understand the real world

In this post, I will show you how to detect the edges in an image. Writing an edge detection program will give you some understanding of computer vision and self driving cars. Edge detection is commonly used to understand objects in an image. It also helps the machines to make better predictions. Writing an edge detection program is a great way to understand how machines see the outside world. This will give us a better perspective when it comes to computer vision.

In my previous articles, I covered couple computer vision concepts: face detection, face recognition and text recognition. And today, we will work on edge detection using python.


Table of Contents

  • Getting Started
  • Import Libraries
  • Edge Detection Function
  • Choose an image
  • Run the program

Getting Started

We will use two main modules for this project, and they are Numpy, Matplotlib and OpenCV. OpenCV is a highly optimized library with a focus on real-time applications.

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

https://matplotlib.org

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

https://opencv.org

Libraries

We have to install the libraries so that our program works properly. As mentioned earlier, we will need only two libraries.  

We can install them in one line using PIP library manager:

pip install numpy matplotlib opencv-python

After the installation process is completed, we can import them to our code. You can work on a Jupiter notebook or a regular text editor like Atom. I will use Atom text editor for this project.

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

Edge Detection Function

Now, we can move to the fun part where we will write the edge detection function. You will be amazed how simple it is using the OpenCV package. This OpenCV detection model is also known as the Canny edge detection model. Our function is consisting of three parts: edge detection, visualization and lastly saving the result.

Edge Detection

def simple_edge_detection(image):
   edges_detected = cv2.Canny(image , 100, 200)
   images = [image , edges_detected]
  • Canny is the method we are calling to do the edge detection using opencv.
  • Image is parameter of the function, which means we will pass the image when calling the function. This way you can test your program with different images easily.
  • 100 and 200 are the minimum and maximum values in hysteresis thresholding
  • Lear more about Canny edge detection (Official Documentation)

Visualization

location = [121, 122]
for loc, ed_image in zip(location, edges_detected):
   plt.subplot(loc)
   plt.imshow(ed_image, cmap='gray')
  • Location array is needed for the plotting part
  • And then we are visualization both the original image and the edge detected image
  • Cmap parameter is used to change the color of the images. In our case, we are converting them to gray

Save the Result

In this final part of the function, we will save the edge detected image and the comparison plot. Thanks to OpenCv and Matplotlib packages; imwrite and savefig functions does the saving for us. And in the last line, show function is going to show us the plot that was created.

cv2.imwrite('edge_detected.png', edges_detected)
plt.savefig('edge_plot.png')
plt.show()

Choose an Image

This will be an easy image. We will find an image that we want to test our canny edge detection program. I will use a the free stock photography page to find couple good images. Here is the link for their website.

After downloading the images, make sure to put them inside the same folder with your project. This will help to import them the program easily. Let’s define an image variable and import the image. Here is how to read an image using OpenCV:

img = cv2.imread('test_image.jpg', 0)  

Here the image that I will test the model:

Photo by João Vítor Heinrichs from Pexels

Run the program

The best and the last step, it’s time to run the program. So far there is nothing that triggers the function. We have to call the function so that the magic happens. Oh, also don’t forget to pass your image as a parameter. Let’s call the function:

 simple_edge_detection(img)

Here are some examples:


Well done! You have created an edge detection model using Python. Python is a very powerful language and things you can create with python is limitless. Now, you also have an idea of how to use computer vision in a real project. Working on hands-on programming projects like this one is the best way to sharpen your coding skills.

I am so glad if you learned something new today. Feel free to contact me if you have any questions while implementing the code. 😊

Follow my blog and youtube channel to stay inspired. Thank you,


More Machine Learning Projects

Rendering Text on Video using Python

Extracting Speech from Video using Python

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: