Updated April 10, 2023
Introduction to OpenCV sobel operator()
The OpenCV sobel operator() is a command which is present in the OpenCV library for Python programming language which is used in order to enable the user for the detection off the edges that I present in an image in both vertical directions as well as horizontal direction. It is a very essential function as detection of edges within an image is one of the most fundamental operations that are involved while have image processing is being performed. the use of the OpenCV sobel operator command helps us introducing the total amount of pixels (data being fed) to be processed by the system and aids in maintaining the structural dimensions and aspect of the image. The most commonly used edge detection methods or schemes are – Laplacian sobel (this is very sensitive towards the noise present in the image; second order derivate based edge detector) and the gradient sobel which is the 1st order based derivative kind of edge detector. Both of these schemes of edge detection methods work in a similar manner and deliver the same amount of intricacy with edge detection.
Syntax for OpenCV sobel operator()
Following is the syntax which has to be used in order to make utilization of the function OpenCV sobel operator():
Sobel * (* src *, * * * dst *, * * * ddepth *, * * * dx *, * * * dy *) *
Parameters for OpenCV sobel operator() function:
The following parameters are accepted by the OpenCV sobel operator function:
Parameter | Description of the Parameter |
Src | This parameter is used for representing the image that is being inputted by the user or the source image. It is an object belonging to the class Mat. |
Dst | this parameter is used for representing the final output image which is produced by the system after the function has been performed on it or Alternatively the destination image. It is an object belonging to the class Mat |
Ddepth | This parameter is used for representing the debt that the image has (-1). it is represented by a variable that has an integral value. |
Dx | this parameter is a variable having an integer value that is used for representing the X derivative. the value of this parameter can be either zero or one. |
Dy | This parameter is a variable having an integer value that is used for representing the Y derivative. The value of this parameter can be either zero or one. |
Return Value | This method is responsible for returning an output image that t is to be loaded from the file which has been specified. |
How does the OpenCV sobel operator work?
There are two ways through which the sobel operator enables edge detection for the images that have been provided by the user. let us discuss both:
Sobel Operator – Edge Detection
it is a method that is based on a gradient with respect to the 1st order of its derivatives. It functions by calculating the first derivative which has been provided for the image, operating separately for the Y-axis and the X-axis. the operator makes use of two kernels of dimensions 3 by 3 that are convolved along with the image provided by the user. In order for calculating the approximate value for the derivatives both for the vertical as well as the horizontal. the below image helps us understand how do Sobel kernel values are taken for y-dir and x-dir:
Sobel Operator – Laplacian-Edge Detection
Unlike the prior method of edge detection here only one kernel is used in order for calculating the values that had passed in a single go for this 2nd order derivatives. 1 Colonel is used with respect to the lap lesion detection method which is demonstrated by the below figure.
the below figure can be used for demonstrating how the diagonals are considered while making use of a single kernel
The below syntax can be used in order for making use of the Laplacian edge detection command using the OpenCV library
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * cv2.Laplacian * (* src *, * * * d * depth, other * * * options. *..) * * * * * * * * * * * * * *
Example of using OpenCV Sobel Operator
# command used to import the OpenCV library to utilize OpenCV read image function
import cv2
# command used to import the numpy library to utilize np_array commands
import numpy as np1
from matplotlib import pyplot as plt2
# command used for reading an image from the disk that has been instructed by the user
#user selects an image with multiple edges and curved line with varying colour intensities
img1 = cv2.imread(Glass painting with multiple edges.jpg',)
# converting the image in to a gray scale image
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# removing the noice from the image
imgT = cv2.GaussianBlur(gray1,(30,30),0)
# convoluting along with kernels
laplacian1 = cv2.Laplacian(imgT, cv2.CV_64F)
sobelx1 = cv2.Sobel(imgT,cv2.CV_64F,1,0,ksize=5) # x-axis
sobely1 = cv2.Sobel(imgT,cv2.CV_64F,0,1,ksize=5) # y-axis
plt2.subplot(2,2,3),plt2.imshow(sobelx1,cmap = * 'gray1')
plt2.title('Sobel X-Axis'), plt2.x * ticks([]),* * * plt2.y * ticks([])
plt2.subplot(2,2,4),plt2.imshow(sobely1,cmap = 'gray1')
plt2.title('Sobel Y-Axis'), plt2.x * ticks([]), plt2.y * ticks([])
plt2 *. * subplot * (20,20,10), * plt2 *.imshow * (imgT, * cmap * = * 'gray1')
plt2.title('Original'), * plt2.xticks([]), plt2. y * ticks * ([*])
plt2.subplot(20,20,20),plt2.imshow(laplacian1,cmap = 'gray1')
plt2.title('Laplacian Edge Detection View'), plt2.x * ticks([]), plt2.y * ticks([])
plt2.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
The original image used for detecting the edges:
Output image with the application of OpenCV sobel operator() in version relevant to Sobel X
Output image with the application of OpenCV sobel operator() in version relevant to Sobel Y
Output image with the application of OpenCV sobel operator() in version relevant to Laplacian Edge detector mode.
Conclusion
The OpenCV sobel operator() is a very essential function as detection of edges within an image is one of the most fundamental operations that are involved while have image processing is being performed. the use of the OpenCV sobel operator command helps us introducing the total amount of pixels (data being fed) to be processed by the system and aids in maintaining the structural dimensions and aspect of the image.
Recommended Articles
We hope that this EDUCBA information on “OpenCV sobel operator()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.