Python programming blog

Thursday, April 1, 2021

Edge detection using python (Open-CV)

Hello python programmers, This is AK, In this blog, we’re going to learn about

 how to create an edge detection program using python. So in this blog, 

we’re going to use three python libraries to detect the edges in the input images.

 So the libraries that we’re using here are open-CV,numpy, and matplotlib. So

 using these three libraries we’re going to detect the edges.

 

The edge detection project is considered a mini project in python programming. 

Those who’re searching for mini projects in python. You can take this project.

 

Why we need edge detection?


Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness. So based on the image brightness and gray factors it detects the edges in the input image. Edge detection is used for image segmentation and data extraction in areas such as image processing, computer vision, and machine vision.

And the applications of edge detection are the finger-print scanners in mobile phones, satellite image processing & medical science fields. These are the

 areas the edge detection technology is highly created an impact.

 

First, you need to install three libraries on your terminal So install the requirements

 on your command prompt.

After installing the requirements you need to import these three libraries.

Here we’re applying the canny edge detector algorithm that is present in the

 Open-CV library in  python

 

Libraries required:

 

pip install open-CV

pip install matplotlib

 

The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. It was developed by John F. canny in 1986. Canny also produced a computational theory of edge detection explaining how this technique works.

This is the small intro about the canny edge detection algorithm. If you want more details about this algorithm check out Wikipedia they gave more detailed information about this algorithm.

 

Step 1:

import cv2

import numpy as numpy

from matplotlib import pyplot as plt 

 

img=cv2.imread('Your Image',0) 

 

After importing all the libraries, declare a variable called an image and

 in this variable pass the input image that you want to detect. 

 

Step 2: 

 

After that, declare a variable called edges and you’ve to set the image 

height and width in the object canny.

edges=cv2.Canny(img,100,200) 

 

Step 3: 

Next, you need to plot the image by using the matplotlib library. For detecting the

 edges you’ve to convert the input image into gray to identify the brightness that 

presents in the edges.

After converting the image you need to set the title as the original image.

 This is for to ensure the difference between the original image and the edged image.

This step is only for plotting the original image and for plotting the edged image 

you’ve to pass the variable edges in the plot method.

So that’s all about the code, let’s run this program 

 

plt.subplot(121),plt.imshow(img,cmap='gray')

plt.title('Original image'),plt.xticks([]),plt.yticks([])

plt.subplot(122),plt.imshow(edges,cmap='gray')

plt.title('Edge Image'),plt.xticks([]),plt.yticks([])

plt.show()

 

Output : 

 

  
 
 

Full code : Edge detection using python

 

Thanks for reading💗 
 


 

 

 

No comments:

Post a Comment