Skip to main content

Canny edge detector

Canny edge detector  with python code.
                                               Canny Edge detection in one traditional method to find the edges of image, it one important task in image processing. Human Eye can able identify the objects in fractions of seconds. But can we implement in computer visiion, Canny edge detection is a technique to extract useful structural information from different vision objects and dramatically reduce the amount of data to be processed. It has been widely applied in various computer vision systems and Artificial intelligent. Canny use calculus of variations - a technique which finds the function which optimizes a given functional.
                                                   Lets we see step by step process.

Removing or Reducing Noise from Image
                                         Pixel in image have different intensity value , The noise removal algorithms reduce or remove the visibility of noise by smoothing the entire image leaving areas near contrast boundaries.  We can smooth image by applying Gaussian blur. To smooth the image, a Gaussian filter kernel is convolved with the image. This step will slightly smooth the image to reduce the effects of obvious noise on the edge detector. The equation for a Gaussian filter kernel of size (2k+1)×(2k+1) is given by:
Here is an example of a 5×5 Gaussian filter, used to create the adjacent image, with {\displaystyle \sigma }\sigma  = 1. (The asterisk denotes a convolution operation.)
Finding Intensity Gradient of the Image
                                       An image gradient is a directional change in the intensity or color in an image.An edge in an image may point in a variety of directions, so the Canny algorithm uses four filters to detect horizontal, vertical and diagonal edges in the blurred image. After smoothing the image ,the edge can be detect by changing the intensity of pixel by Sobel kernels .  
 The edge detection operator (such as Roberts, Prewitt, or Sobel) returns a value for the first derivative in the horizontal direction (Gx) and the vertical direction (Gy). From this the edge gradient and direction can be determined:
,
gradient_magnitude = np.sqrt(np.square(sobelx) + np.square(sobely))
gradient_magnitude *= 255.0 / gradient_magnitude.max()
where G magnitude and θ  is slope or angle of pixel. 

The python coding
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('sobel.jpg',0)
laplacian = cv.Laplacian(img,cv.CV_64F)
sobelx = cv.Sobel(img,cv.CV_64F,1,0,ksize=5)
sobely = cv.Sobel(img,cv.CV_64F,0,1,ksize=5)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()
gradient_magnitude = np.sqrt(np.square(sobelx) + np.square(sobely))
gradient_magnitude *= 255.0 / gradient_magnitude.max()
plt.imshow(gradient_magnitude,cmap = 'gray')
plt.show()


Gradient Magnitude Image

Non-Maximum Suppression
                                       In this step,remove pixel that are unwanted. We can choose pixel what was maximum intensity to it neighbor intensity value. 
Maxima and minima Threshold 
                                       Pixel is consider as edge pixel when it intensity value greater than maximum threshold and pixel below minima are discarded. The two threshold values are empirically determined and their definition will depend on the content of a given input image.
Edge Tracking by Hysteresis
                                   Now there some pixel(weak pixel) which are between maxima and minima threshold. To get accurate edge, we convert some weak pixel to strong pixel. 

Original Image

The Python code

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('sobel.jpg',0)
edges = cv2.Canny(img,100,200)
plt.subplot(122),plt.imshow(edges,cmap = 'hsv')
plt.title('Canny Edge Image')
plt.show()






Comments