Edge Detection using Python

Machine learning is the vast subject these days where every day new experiments and research is doing. Python is the most promising and advanced programming language which is very suitable for engineering and scientific applications that’s why I am using python programming for an edge detection technology with an Anaconda Package.

Here we are using multiple libraries like numpy, scipy and matplotlib etc. Anaconda package comes with all types of needed libraries so you don’t need to install the separate library for every functionality.

Why Edge Detection is Useful?

An edge may be defined as a set of connected pixels that forms a boundary between two disarrange regions. Edge detection is a method of segmenting an image into regions of conclusion.

Edge detection plays a very important role in digital image processing and the practical aspects of our life. In this report, we studied various edge detection techniques as Robert, Sobel and Canny operators.

On comparing them we can see that canny edge detector performs better than all other edge detectors on various aspects such as it is flexible in nature, better for noisy image and gives sharp edges, low probability of detecting false edges.

So canny edge detection is best among all edge detection techniques that’s why I am including only canny edge detection on this blog post.

I have divided this article into 4 steps, where we will import the needed libraries and then read the image and perform different actions on it. Like grayscale, tint and binary conversion. In the last step, a canny edge detection method will be applied.

1.) Import Libraries: Here we will import all needed libraries like numpy, scipy, matplotlib etc.

#importing all data
import numpy as np
from scipy.misc import imread, imresize
from skimage import color
import matplotlib.pyplot as plt
from skimage import feature

2.) Read the image and apply different image processing techniques on an Image

# Image Define
img = imread('img/divyang.jpg') #Read the image
img_gray =color.rgb2gray(img) * 255  #Convert the image yo grayscale
img_tinted = img * [0.25, 0.45, 0.45] #Convert image into tint/hue image
edges = feature.canny(img_gray,1,120) #Edge detection

3.) Display different processed images with the subplot functions.

# Show the original image
plt.subplot(1, 2, 1) #subplot function is used to create a Graph Area
plt.imshow(img)
plt.title('Orignal Image')

4.) Canny edge Detection function

 
edges = feature.canny(img_gray,1,120) #Edge detection
Here is the full code of edge detection.
#importing all data

import numpy as np
from scipy.misc import imread, imresize
from skimage import color
import matplotlib.pyplot as plt
from skimage import feature



# Image Define
img = imread('img/divyang.jpg') #Read the image
img_gray =color.rgb2gray(img) * 255  #Convert the image yo grayscale
img_tinted = img * [0.25, 0.45, 0.45] #Convert image into tint/hue image
edges = feature.canny(img_gray,1,120) #Edge detection


# Show the original image
plt.subplot(1, 2, 1) #subplot function is used to create a Graph Area
plt.imshow(img)
plt.title('Orignal Image')

# Show the tinted image
plt.subplot(1, 2, 2)
plt.title('Tinted image')

# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()

plt.subplot(1, 2, 1)
plt.imshow(img_gray, cmap="gray")
plt.title('Gray Image')

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

Original Image
canny edge detection

Canny Edge Detection:

canny edge detection
For more information related to edge detection technology you can read my full research paper on academia Edge Detection Technology

Leave a Reply

Your email address will not be published. Required fields are marked *