How to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?


Using the Sobel operator, we can compute image gradients in horizontal as well as vertical direction. The gradients are computed for a grayscale image. The Laplacian operator computes the gradients using the second-order derivatives.

Syntax

The following syntaxes are used to compute the image gradients using Sobel and Laplacian derivatives −

cv2.Sobel(img, ddepth, xorder, yorder, ksize)
cv2.Laplacian(img, ddepth)

Parameters

  • img − The original input image.

  • ddepth − Desired depth of the output image. It has information about what kind of data is stored in the output image. We use cv2.CV_64F to as ddepth. It is a 64bit floating-point opencv.

  • xorder − The order of derivatives in horizontal direction (X-direction). Set xorder=1, yorder=0 for the 1st order derivative in X-direction.

  • yorder − The order of derivatives in vertical direction (Y-direction). Set xorder=0, yorder=1 for the 1st order derivative in y-direction.

  • ksize − The kernel size. Set ksize=5 for a 5×5 kernel size.

Steps

You can use the following steps to find image gradients using Sobel and Laplacian derivatives −

Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.

import cv2

Read the input image using cv2.imread() as a grayscale image.

img = cv2.imread('lines.jpg',0)

Compute the Sobel or Laplacian derivative using cv2.Sobel() or cv2.Laplacian(). This derivative refers to the image gradient.

sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)

Display the image gradient using cv2.imshow() method.

cv2.imshow("Sobel X", sobelx)
cv2.waitKey(0)
cv2.destroyAllWindows()

We will use this image as the input file in the following examples.

Example 1

In the Python program below, we compute the image gradient using 1st order Sobel derivatives in X and Y directions, i.e., horizontally and vertically, respectively. We use a kernel size of 5×5.

# import required libraries import cv2 import numpy as np from matplotlib import pyplot as plt # read the input image as a grayscale image img = cv2.imread('lines.jpg',0) # compute the 1st order Sobel derivative in X-direction sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # compute the 1st order Sobel derivative in Y-direction sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5) # display sobelx and sobely cv2.imshow("Sobel X", sobelx) cv2.waitKey(0) cv2.imshow("Sobel Y", sobely) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When you run the above program, it will produce the following two output windows. Sobel X window shows the derivatives in X-direction (horizontal) and Sobel Y window shows the derivatives in Y-direction (vertical).

Example 2

In the Python program below, we compute image gradients using Laplacian derivatives.

# import required libraries import cv2 import numpy as np from matplotlib import pyplot as plt # read the input image as a grayscale image img = cv2.imread('lines.jpg',0) # compute Laplacian derivative of the input image laplacian = cv2.Laplacian(img,cv2.CV_64F) # display the laplacian cv2.imshow("Laplacian", laplacian) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When you run the above program, it will produce the following output window.

Updated on: 27-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements