Sobel and Scharr Operator in OpenCV

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In this article, we’ll understand how we can compute the gradient of an image by using the operators provided in the OpenCV library. We will see what are sobel and scharr operator in OpenCV and their functioning. But firstly, let’s see what is image gradient.

What is Image Gradient in Open CV?

An image gradient can be defined as the directional change in the intensity or color of an image. The magnitude of the gradient describes the speed at which the image is changing, while the direction of the gradient describes the direction in which the image is changing rapidly. Sobel and Scharr’s kernels are convolution operators that compute the image gradients.

Use of gradient in image processing

1. Computing image gradients is a necessary step of many computer vision and image processing routines.

2. Gradients are used for edge detection in images, using which contours and outlines of objects in images are found.

3. Gradients are used as inputs for quantifying images through feature extraction.

4. Saliency maps, which highlight the subjects of an image, are constructed using gradients.

Theory of edge detection in image processing

1. An edge in an image is represented by a sudden change in the intensity value of the pixel. This change in intensity value can be described by derivatives. The greater is the change in gradient, the larger is the change in the image.

2. In a 1-dimensional image, the edge can be represented by a jump in the intensity value. This jump can be represented graphically as:

sobel scharr operators

3. The jump in the intensity value can be observed more carefully by calculating the first derivative. The graph below represents the first derivative as the maximum value.

sobel scharr operators in opencv

4. The edge detection method works on the principle of identifying pixels in the image with a higher gradient value than its neighbors.

Formulation of Sobel and Scharr operators in OpenCV

Assuming that the image we are operating on is

1. Two derivatives are calculated

a. Horizontal derivative: The horizontal derivative of image I is computed by convolving the image I with a kernel Gx. The kernel has an odd size.

The kernel Gx is given by:
Gx =

-1 0 1
-2 0 2
-1 0 2

* I

(matrix * I)

b. Vertical derivative: The vertical derivative of image I is computed by convolving the image I with a kernel Gy. The kernel has an odd size.

The kernel Gy is given by:

-1 -2 -1
0 0 0
1 2 1

* I

(Matrix * I)

2. For every point of the image, approximation of the gradient at that point is calculated by using the value of the kernels Gx and Gy

G = Sq Root of (Gx2+Gy2)

Or G = | Gx |+|Gy|

Sobel operator in OpenCV

The Sobel operator sometimes called the Sobel–Feldman operator or Sobel filter is used in image processing and computer vision, particularly within edge detection algorithms where it emphasizes the edges.

It is a discrete differentiation operator. It is used to determine the approximation of the gradient of an image intensity function. The Sobel Operator is a combination of Gaussian smoothing and differentiation.

It determines the first, second, third, or mixed image derivatives. To calculate the derivative, ksize X ksize separable kernel is used.

The Sobel operators use two 3 X 3 kernels which are convolved with the original image to calculate approximations of the derivatives – one for horizontal changes and one for vertical.

Gradients can be calculated in a specific direction such as normalized X-direction gradient (it shows us the vertical edges) or the normalized Y-direction gradient (it shows us the horizontal edges) using the Sobel operator. Normalized gradient magnitude from the Sobel-Feldman operator shows us both the vertical and horizontal edges.

In simpler terms, we know that gradients are a change in color or intensity. So, change has to do with derivatives. We are calculating the rate of change and the kernels can calculate approximations of those changes.

OpenCV Sobel Derivatives:

The Sobel gradient for an image can be calculated in different directions depending on the value of the dx and dy parameters

1. X-direction Sobel derivative: The Sobel derivative is computed in the X-direction by setting the value for the x derivative as 1 and the value for y derivative as 0. Therefore, dx will be equal to 1 and dy will be equal to 0.

2. Y-direction Sobel derivative: The Sobel derivative is computed in the Y-direction by setting the value for the x derivative as 0 and the value for y derivative as 1. Therefore, dx will be equal to 0 and dy will be equal to 1.

3. X and Y direction Sobel derivative: The Sobel derivative can be computed in both the X direction and Y direction by setting the value for the x derivative and y derivative as 1. Therefore, dx and dy will be equal to 1.

Syntax

cv2.Sobel(src, dst, ddepth, dx, dy, ksize)

Parameters

  • src: The source image
  • dst: Output image
  • ddepth: Depth of output image
  • dx: The order of derivative x
  • dy: The order of derivative y
  • ksize: Size of the kernel

Implementation

# Importing OpenCV
import cv2
# Importing matplotlib.pyplot
import matplotlib.pyplot as plt
# Reading the image
img = cv2.imread(r'C:\Users\tushi\Downloads\PythonGeeks\suduko.jpg')
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Displaying the original image
plt.imshow(img, cmap='gray')

original

# X gradient Sobel
# Depth is the 'precision' of each pixel. It can be 8/24/32/64 for displaying.
sobelx =  cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
# Displaying the output image
plt.imshow(sobelx, cmap='gray')

sobel x

# Y gradient sobel
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
# Displaying the output image
plt.imshow(sobely, cmap='gray')

sobel y

Scharr operator in OpenCV

The Scharr operator is used as a method to identify and highlight gradient edges or features of an image using the 1st derivative. It is commonly used to identify gradients along the x-axis (dx = 1, dy = 0) and y-axis (dx = 0, dy = 1). The performance of the Scharr operator is quite similar to the Sobel operator.

The Scharr operator is an enhancement of the difference between the Sobel operator, and the two are the same as the principle of the edge of the image. It increases the difference between the pixel value by amplifying the weight coefficient.

Scharr Derivatives in OpenCV:

The Scharr gradient for an image can be calculated in different directions depending on the value of the dx and dy parameters

1. X-direction Scharr derivative: The Scharr derivative is computed in the X-direction by setting the value for the x derivative as 1 and the value for y derivative as 0. Therefore, dx will be equal to 1 and dy will be equal to 0.

2. Y-direction Scharr derivative: The Scharr derivative is computed in the Y-direction by setting the value for the x derivative as 0 and the value for y derivative as 1. Therefore, dx will be equal to 0 and dy will be equal to 1.

3. X and Y direction Scharr derivative: The Scharr derivative cannot be computed for both X and Y directions simultaneously.

Syntax

cv2.Scharr(src, dst, ddepth, dx, dy)

Parameters

  • src: The source image
  • dst: Output image
  • ddepth: Depth of output image
  • dx: The order of derivative x
  • dy: The order of derivative y

Implementation

# Importing OpenCV
import cv2
# Importing numpy
import numpy as np
# Importing matplotlib.pyplot
import matplotlib.pyplot as plt
# Reading the image
img = cv2.imread(r'C:\Users\tushi\Downloads\PythonGeeks\suduko.jpg')
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Displaying the original image
plt.imshow(img, cmap='gray')

original

# X gradient Scharr operator
fieldx = cv2.Scharr(img, cv2.CV_32F, 1, 0) / 15.36
# Displaying output image
plt.imshow(fieldx, cmap='gray')

scharr x

# Y gradient Scharr operator
fieldy = cv2.Scharr(img, cv2.CV_32F, 0, 1) / 15.36
# Displaying output image
plt.imshow(fieldy, cmap='gray')

scharr y

Conclusion

Through this article, we understood what we mean by image gradient and why it is necessary for image processing. We understood the theory of edge detection in image processing and also learned the formulation of the Sobel and Scharr operator used to compute the gradient of an image. We also implemented these functions to gain a better understanding of these functions.

Your opinion matters
Please write your valuable feedback about PythonGeeks on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *