Sunday, 17 December 2023

OpenCV: Sobel edge detection

Sobel operation is used for edge detection and gradient computation in Image processing.

 

Sobel operation has two 3X3 convolution kernels.

a.   Kernel1 : For detecting changes in intensity in the horizontal (x) direction

b.   Kernel2: For detecting the changes in vertical direction.

 

Horizontal kernel

-1  0  1

-2  0  2

-1  0  1

 

Vertical Kernel

-1 -2 -1

 0  0  0

1     2  1

 

How to find edges using Sobel operator in OpenCV?

Using cv2.Sobel method, we can apply Sobel operator on an Image.

 

Signature

cv2.Sobel(src, ddepth, dx, dy, ksize[, scale[, delta[, borderType]]])

 

Following table summarizes the parameters of Sobel method.

Parameter

Description

src

Specifies the source image on which you want to apply sobel operator.

ddepth

Specifies desired depth of the image. By default it is set to -1 to indicate output image has same depth as source image.

dx

Order of derivatives in x-axis

dy

Order of derivatives in y-axis

ksize

Specifies the size of Sobel kernel. In general it is set to odd number like 3, 5, 7 etc.,

scale

It is an optional parameter used to scale the result

delta

It is an optional parameter, added to the result.

borderType

It is optional parameter, specifies how to handle border pixels.

 

sobel_edge_detection.py

import cv2 as cv
import numpy as np

image = cv.imread('gateway_of_india.png')
gray_scale_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

sobel_x = cv.Sobel(image, -1, 1, 0)
sobel_y = cv.Sobel(image, -1, 0, 1)
sobel = cv.bitwise_or(sobel_x, sobel_y)

cv.imshow('Original image', image)
cv.imshow('gray_scale_image', gray_scale_image)
cv.imshow('sobel_x', sobel_x)
cv.imshow('sobel_y', sobel_y)
cv.imshow('sobel', sobel)

cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

Original image

 


 

Gray scale image

 


sobel_x




sobel_y

 


 

sobel



Previous                                                    Next                                                    Home

Detect edges using Laplacian operator

Laplacian edge detection uses the Laplacian operator to identify regions of rapid intensity change. 'Laplacian operator' is used to measure the change in the intensity of an image at a pixel.

 

Using cv2.Laplacian method, we can calculate the Laplacian image.

 

Signature

dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])

Following table summarizes the parameters of Laplacian method.

 

Parameter

Description

src

Source image in which you want to apply Laplacian filter.

ddepth

Specifies the depth of destination image. It specifies the desired data type for the output image, which can be one of the following values:

 

cv2.CV_8U: 8-bit unsigned integer.

cv2.CV_16U: 16-bit unsigned integer.

cv2.CV_16S: 16-bit signed integer.

cv2.CV_32F: 32-bit floating-point.

cv2.CV_64F: 64-bit floating-point.

ksize

It is optional parameter, specifies the size of the kernel (filter) that defines the extent of the Laplacian operation

scale

It is optional parameter, specifies the scale factor for the computed Laplacian values.

delta

It is optional parameter, added to the result.

borderType

It is optional parameter, specifies how border of the image is handled. Following values are supported.

 

cv2.BORDER_DEFAULT: Default border behavior.

cv2.BORDER_CONSTANT: Adds a constant border (specified by borderValue).

cv2.BORDER_REFLECT: Reflects the image around the border.

cv2.BORDER_REFLECT_101: Reflects the image around the border with a slight change.

cv2.BORDER_REPLICATE: Replicates the last pixel of the border.

 

Example

laplacian = cv.Laplacian(gray_scale_image, -1)

The result of Laplacian method might have both positive and negative values, so you convert it to an appropriate data type using cv2.convertScaleAbs to ensure it's in the correct format for display.

 

laplacian = cv.convertScaleAbs(laplacian)

 

Find the below working application.

 

laplacian_edge_detection.py

import cv2 as cv
import numpy as np

image = cv.imread('gateway_of_india.png')
gray_scale_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

laplacian = cv.Laplacian(gray_scale_image, -1)
#laplacian = np.uint8(np.absolute(laplacian))
# Convert the result to the appropriate data type and scale it
laplacian = cv.convertScaleAbs(laplacian)

cv.imshow('Original image', image)
cv.imshow('gray_scale_image', gray_scale_image)
cv.imshow('Laplacian image', laplacian)

cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

Output

Original image


 

Gray scale image



Laplacian image

  



Previous                                                    Next                                                    Home

OpenCV: Detect edges using Canny edge detector

‘cv2.Canny’ method is used to detect the edge in a digital image using Canny edge detector algorithm.

Signature

edges = cv2.Canny(image, lowerThreshold, upperThreshold[, edges[, apertureSize[, L2gradient]]])

 

Following table summarizes the parameters of Canny method.

 

Parameter

Description

image

Input image

lowerThreshold and upperThreshold

a.   Pixels with gradient values below lowerThreshold are considered non-edges, and

b.   pixels with gradient values above upperThreshold are considered strong edges.

c.    Pixels with gradient values between lowerThreshold and upperThreshold are considered weak edges and are included if they are connected to strong edges.

edges

It is an optional parameter, represents output image where the detected edges are stored

apertureSize

It is an optional parameter, represent the size of the Sobel kernel used to find the intensity gradient.

L2gradient

It is an optional boolean parameter. If it is set to True, then a more accurate L2-norm gradient calculation should be used. It is set to False by default

 

Example

canny = cv.Canny(image, 120, 150)

 

How to choose lower and upper values in Canny method?

Choosing the right threshold values for the Canny edge detection method is an important step in the process. A higher threshold will result in fewer edges being detected, while a lower threshold will result in more edges being detected. In general,

a.   you can start with lower threshold values and increase them gradually until you are satisfied with the result.

b.   Use a higher upper threshold if you want to detect strong edges only.

c.    Use a lower upper threshold if you want to detect weak edges as well.

d.   Use a higher lower threshold if you want to reduce noise in the output image.

e.   Use a lower lower threshold if you want to detect even faint edges.

 

canny_edge_detection.py

import cv2 as cv

image = cv.imread('gateway_of_india.png')
cv.imshow('Original image', image)

canny = cv.Canny(image, 120, 150)
cv.imshow('Original image with threshold 120 and 150', canny)

canny = cv.Canny(image, 120, 400)
cv.imshow('Original image with threshold 120 and 400', canny)

cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

Original Image

 


 

Edges with thresholds 120 and 150

 


Edges with thresholds 120 and 400

 


 

Previous                                                    Next                                                    Home