×

Edge Detection in OpenCV

Edge detection is an important method in image processing. Edges are important aspects that are associated with images. It involves detecting the boundaries of the objects in the image.

Edge detection involves detecting the change in pixel intensities along the boundaries of the image. We can define the edges in the image using image gradients.

Image gradients are the basic building blocks of computer vision and image processing. Image gradients are the direction of change in intensity of the pixels.

Gradients of an image provide information such as the magnitude of change in intensity and direction of the gradient tells us how rapidly and in which direction the change is occurring. We use gradients to detect edges and thus helps in finding out contours and other outlines of the objects in the image.

For detecting edges we use grayscale images so that adding blurriness and smoothing the object intensities around the corners helps to detect edges. In OpenCV, we can define gradients using

  • cv2.Laplacian()
  • cv2.Sobel()

Laplacian method for detecting gradients

In the Laplacian method, we pass a grayscale image into cv2.Laplacian(), Since it involves checking the change in pixel intensities from white to black and black to white, we find out the change in pixel intensities by passing cv2.CV_64F and return positive and negative pixels.

laplace = cv2.Laplacian(gray_image, cv2.CV_64F)

Now before displaying the image there is one more operation that needs to be performed is converting all the negative values into positive values that we got from cv2.Laplacian() using numpy.absolute() method. Later converting it into image datatype using np.uint8().

# since np.absolute returns positive integers we convert them into 
#image datatype using np.uint8.
laplace = np.uint8(np.absolute(laplace))
import cv2
import numpy as np
image = cv2.imread('C:\images\Nature.jpg')

#we convert BGR image to GRAY image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
laplace = cv2.Laplacian(gray_image, cv2.CV_64F)
laplace = np.uint8(np.absolute(laplace))

cv2.imshow("laplacian image", laplace)

Output:-

Image 58

Sobel Gradient Detection

Sobel detects gradients based on calculating the derivatives of intensity deviation among the pixels, for gradient detection Sobel detects the intensity distribution along X-axis and Y-axis separately and merges both distributions by performing bitwise operations.

Sobel is one of the most important gradients detection techniques since many algorithms that are used for edge detection are either based on the Sobel method or involves the Sobel method at some level.

We calculate intensity deviation by finding its derivative along X-axis using cv2.sobel(img, cv2.CV_64F, dx, dy), if dx =1 and dy = 0 it calculates the Sobel’s derivative along X-axis.

# dx = 1, dy = 0 , thus it calculates along X-axis
sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0)

We calculate intensity deviation by finding its derivative along Y-axis using cv2.sobel(img, cv2.CV_64F, dx, dy), if dx =0 and dy = 1 it calculates the Sobel’s derivative along Y-axis.

# dx = 0, dy = 1 , thus it calculates along Y-axis
sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1)

By performing the bitwise_or operation between Sobel derivatives of X & Y we can get complete gradients that are in the image.

#cv2.bitwise_or(img1, img2)
sobel = cv2.bitwise_or(sobel_x, sobel_y)

The complete source code that involves all operations is

import cv2

image = cv2.imread('C:\images\Nature.jpg')

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0)
sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1)
cv2.imshow("gradient_X", sobel_x)
cv2.imshow("gradient_Y", sobel_y)

sobel = cv2.bitwise_or(sobel_x, sobel_y)

cv2.imshow("actual image", image)
cv2.imshow("gradient", sobel)

Output:-

Image 60
Image 59

Canny Edge detection

Canny edge detection is one of the most important methods that is used for detecting the edges that are present in an image. Canny Edge is one of the most sophisticated and important algorithms and is a multi-stage process algorithm. Sobel is used in processing Canny.

Canny Edge detection is built on different layers of algorithms in which one of the layers is Sobel. Canny provides smoother and much better edges as the process involves a gray image and adding Gaussian blur.

Adding Gaussian Blur with a kernel size of ( 3, 3 )

blur = cv2.GaussianBlur(gray_image, (3, 3), cv2.BORDER_DEFAULT)

Apply canny to the blurred image using cv2.Canny()

# takes parameters such as aperture min size and max size
canned = cv2.Canny(gray_image, 150, 190)
import cv2
image = cv2.imread('C:\images\Nature.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray_image, (3, 3), cv2.BORDER_DEFAULT)
canned = cv2.Canny(gray_image, 150, 190)
cv2.imshow("actual image", image)
cv2.imshow("canned image", canned)

Output:-

Image 65