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
No comments:
Post a Comment