Find the Gradient of an Image in Python

In this tutorial, we will learn how to find the gradient of the image in python. After this tutorial able to find the gradient of an image in X, Y, both directions and use of some important libraries.

What is a Gradient of the image?

Image of the gradient is nothing but the change of intensity of image colors in X, Y or both directions.

We can find the gradient of an image by the help of Sobel and Laplacian derivatives of the image. Sobel is used for either X or Y direction or even in combined form while Laplacian help in both directions.

Installation Of Libraries

Don’t worry about the mathematical calculation of the image. Here, In Python, we have Library named OpenCV(open computer vision) for image processing. We need two more libraries one is Numpy and Matplotlib. We need to install matplotlib or OpenCV in our system while with OpenCV, Numpy installed by default.

In Windows & Linux :

Fired the following command on your command prompt.

   pip install opencv-python

   pip install matplotlib

Program For Gradient Of Image In Python

First, we import our libraries cv2 of OpenCV, Numpy and Matplotlib. After that, we read our image using imread() function of cv2 with two parameters first one is our image name with extension(make sure that image is in the same folder) and another one is scale type which is either 0,1,-1 only in numeric form. This three scale type refers to grayscale, color or nochange.

import cv2
import numpy as np
import matplotlib.pyplot as plot
image = cv2.imread("logo_og.png",0)

After that, we will calculate the Laplacian derivatives of the image using Laplacian() by passing three parameters first one is our image variable, a second one data type which is CV_64F of cv2, and third is kernel size ksize = 3(make sure always use odd number) ksize increases the thickness of edges. ksize 3 will give you the best result. After this, we will convert the value into an unsigned 8-bit integer of uint8 of Numpy.

After that as same as we calculate the gradient of the image in x or y direction using Sobel() of cv2 by passing four parameters first one is image source, the second one is depth, the third one is derivates in the x-direction and fourth one is y  derivatives in y-direction dy.

# Below code convert image gradient in both x and y direction
lap = cv2.Laplacian(image,cv2.CV_64F,ksize=3) 
lap = np.uint8(np.absolute(lap))

# Below code convert image gradient in x direction
sobelx= cv2.Sobel(image,0, dx=1,dy=0)
sobelx= np.uint8(np.absolute(sobelx))

# Below code convert image gradient in y direction
sobely= cv2.Sobel(image,0, dx=0,dy=1)
sobely = np.uint8(np.absolute(sobely))

In last, we create two lists one for the title and another one for images generated by the laplacian, sobelx and sobely. After using matplotlib we plot on a screen using imshow() method by passing two parameters first for image source and the second one for the background.  yticks() and xticks() may be passed with a list(maybe empty too) used to set the labels in x or y directions.

results = [lap,sobelx,sobely]
images =["Gradient Image","Gradient In X direction","Gradient In Y direction"]
for i in range(3):
    plot.title(results[i])
    plot.subplot(1,3,i+1)
    plot.imshow(results[i],"plasma")
    plot.xticks([])
    plot.yticks([])

plot.show()

Input :

Find the Gradient of an Image in Python


Output  :

Find the Gradient of an Image in Python

Leave a Reply

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