Beginner’s Guide To Image Gradient

Shivansh Kaushal Last Updated : 12 Nov, 2024
10 min read

Over the past few decades, AI technologies have greatly advanced. We can see the heavy usage of these technologies almost everywhere, from the healthcare industry to self-driving cars to completely automated machines. Computer vision is artificial intelligence that enables computers to derive information from images, videos, and other inputs. Before jumping into this cutting-edge technology, let’s discuss the basic building blocks of many edge detection algorithms, such as Image Gradient.

Overview:

  • Learn about the fundamentals of Image Gradients
  • Gain an understanding of the significance of Image Gradients in edge detection
  • Discover the mathematical computation of Image Gradients
  • Explore the use of different edge detection filters
  • Implement edge detection using OpenCV

What is an Image Gradient?

Let’s understand the term “Image Gradient” first. An image is a matrix of pixel values representing various intensity level values. A pixel is the building block of an image. The gradient can be defined as the change in the direction of an image’s intensity level.

Image Gradient

The gradient helps us measure how the image changes. Based on sharp changes in the intensity levels, it detects the presence of an edge. We will dive deep into it by manually computing the gradient momentarily.

Why Do We Need an Image Gradient?

Image gradients are used to extract information from an image. They are one of the fundamental building blocks in image processing and edge detection. The main application of gradient of an image is edge detection. Many algorithms, such as Canny Edge Detection, use image gradients to detect edges.

Also Read: Comprehensive Guide to Edge Detection Algorithms

Mathematical Calculation of Image Gradients

Enough talking about gradients. Let’s now look at how we compute gradients manually. Let’s take a 3*3 image and try to find an edge using an image gradient. We will start by taking a centre pixel around which we want to detect the edge. We have four main neighbours of the centre pixel, which are:

(i) P(x, y-1) top pixel
(ii) P(x+1,y) right pixel
(iii) P(x-1,y) left pixel
(iv) P(x,y+1) bottom pixel

We will subtract the pixels opposite each other, i.e., Pbottom—Ptop and Pright—Pleft, which will give us the change in intensity or the contrast in the level of intensity of the opposite pixel.

Change of intensity in the X direction is given by:

                  Gradient in Y direction = PR - PL

Change of intensity in the Y direction is given by:

                  Gradient in Y direction = PB - PT

Gradient for the image function is given by:

                 𝛥I = [𝛿I/𝛿x, 𝛿I/𝛿y]

Gradient of the Image

Let us find out the gradient for the given image function:

image gradient

We can see from the image above that there is a change in intensity levels only in the horizontal direction and no change in the y direction. Let’s try to replicate the above image in a 3*3 image, creating it manually-

image gradient

Let us now find out the change in intensity level for the image above

GX = PR - PL Gy = PB - PT
GX = 0-255 = -255
Gy = 255 - 255 = 0

Gradient for this Image function will be:
𝛥I = [ -255, 0]

Let us take another image to understand the process clearly.

Gradient

Let us try replicating this image using a grid system and creating a similar 3 * 3 image.

Gradient

Also , Read about this article Gradient and its types

Now we can see that there is no change in the horizontal direction of the image

GX = PR - PL , Gy = PB - PT

GX = 255 - 255 = 0
Gy = 0 - 255 = -255

The gradient for this Image function will be:
𝛥I = [0, -255]

But what if the intensity level changes in both directions of the image? Let us take an example where the image intensity changes in both directions.

image intensity

Let us now try replicating this image using a grid system and create a similar 3 * 3 image.

image intensity
GX = PR - PL , Gy = PB - PT

GX = 0 - 255 = -255
Gy = 0 - 255 = -255

Gradient for this Image function will be:
𝛥I = [ -255, -255]

Now that we have found the gradient values let me introduce you to two new terms:

  • Gradient magnitude
  • Gradient orientation

Gradient magnitude represents the strength of the change in the intensity level of the image. The given formula calculates it:

Gradient Magnitude: √((change in x)² +(change in Y)²)

The higher the Gradient magnitude, the stronger the change in the image intensity

Gradient Orientation represents the direction of the change of intensity levels in the image. We can find out gradient orientation by the formula given below:

Gradient Orientation: tan-¹( (𝛿I/𝛿y) / (𝛿I/𝛿x)) * (180/𝝅)

Read More About the Gradient Descent Algorithm in this article

Overview of Filters

We have learned to calculate gradients manually, but we can’t do that manually every time, especially with large images. We can find the gradient of any image by convoluting a filter over it. To find the orientation of the edge, we have to find the gradient in both X and Y directions and then find the resultant of both to get the very edge.

Different filters or kernels can be used to find gradients, i.e., detect edges.

Three filters that we will be working on in this article are

  • Roberts filter
  • Prewitt filter
  • Sobel filter

All the filters can be used for different purposes. They are similar to each other but different in some properties. All these filters have horizontal and vertical edge detection filters.

These filters differ in terms of the value orientation and size

Roberts Filter

Roberts Filter

Roberts filter is a 2 * 2 filter. It is one of the oldest and simplest filters. The idea behind the Roberts cross operator is to approximate the gradient of an image through discrete differentiation, computed by summing the squares of the differences between diagonally adjacent pixels.

We will now examine how to find gradients of an image using filters. We will start with the Roberts filter.
Suppose we have this 4*4 image.

Roberts Filter

Now, will take both the Gx and Gy filters and convolute them over the image.

Let us look at the computation

The gradient in x-direction =

Gx and Gy filters
Gx = 100 *1 + 200*0 + 150*0 - 35*1
Gx = 65

The gradient in y direction =

gradient in y direction
Gy = 100 *0 + 200*1 - 150*1 + 35*0
Gy = 50

We can calculate gradient strength and orientation now that we have discovered both these values.

Gradient magnitude = √(Gx)² + (Gy)²
= √(65)² + (50)² = √6725 ≅ 82

We can use the arctan2 function of NumPy to find the tan-1 to find the gradient orientation.

Gradient Orientation = np.arctan2( Gy / Gx) * (180/ 𝝅)
= 37.5685

Clear your understanding on Gradient Descent in Linear Regression

Prewitt Filter

The Prewitt filter is a 3 * 3 filter, which is more sensitive to vertical and horizontal edges than the Sobel filter. It detects two types of edges: vertical and horizontal. Edges are calculated using the difference between an image’s corresponding pixel intensities.

Prewitt Filter

A working example of Prewitt filter

Let’s calculate the image gradient using the Prewitt filter
Suppose we have the same 4*4 image as earlier

same 4*4 image

Now, I will take both the Gx and Gy filters and convolute them over the image.
Let us look at the computation

The gradient in x direction =

gradient in x direction
Gx = 100 *(-1) + 200*0 + 100*1 + 150*(-1) + 35*0 + 100*1 + 50*(-1) + 100*0 + 200*1
Gx = 100

The gradient in y direction =

Roberts Filter
Gy = 100 *1 + 200*1 + 200*1 + 150*0 + 35*0 +100*0 + 50*(-1) + 100*(-1) + 200*(-1)
Gy = 150

Now that we have found both these values let us calculate gradient strength and gradient orientation.

Gradient magnitude = √(Gx)² + (Gy)²
= √(100)² + (150)² = √32500 ≅ 180

We will use the arctan2 function of NumPy to find the gradient orientation

Gradient Orientation = np.arctan2( Gy / Gx) * (180/ 𝝅)
= 56.3099

Sobel Filter

The Sobel filter is the same as the Prewitt filter, and only the center 2 values are changed from 1 to 2 and -1 to -2 in both filters used for horizontal and vertical edge detection.

Sobel Filter

A working example of a Sobel filter

Let’s calculate the image gradient using the Sobel filter
Suppose we have the same 4*4 image as earlier

Now, I will take both the Gx and Gy filters and convolute them over the image.
Let us look at the computation

The gradient in x direction =

Gradient in X Direction
Gx = 100 *(-1) + 200*0 + 100*1 + 150*(-2) + 35*0 + 100*2 + 50*(-1) + 100*0 + 200*1
Gx = 50

The gradient in y direction =

Sobel Filter
Gy = 100 *1 + 200*2 + 100*1 + 150*0 + 35*0 +100*0 + 50*(-1) + 100*(-2) + 200*(-1)
Gy = 150

Now that we have discovered both these values, let us calculate gradient strength and orientation.

Gradient magnitude = √(Gx)² + (Gy)²
= √(50)² + (150)² = √ ≅ 58

Using the arctan2 function of NumPy to find the gradient orientation

Gradient Orientation = np.arctan2( Gy / Gx) * (180/ 𝝅)
= 71.5650

Implementation using OpenCV

Let us implement the OpenCV code for Image gradient using the abovementioned filters.

We will perform the program on a very famous image known as Lenna.

Click the link below to download the image:

Download
Let us start by installing the OpenCV package

#installing opencv
!pip install cv2

After we have installed the package, let us import the package and other libraries

Using Roberts filter

Python Code:

Using Prewitt Filter

#Converting image to grayscale
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#Creating Prewitt filter
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])

#Applying filter to the image in both x and y direction
img_prewittx = cv2.filter2D(img, -1, kernelx)
img_prewitty = cv2.filter2D(img, -1, kernely)

# Taking root of squared sum(np.hypot) from both the direction and displaying the result
prewitt = np.hypot(img_prewitty,img_prewittx)
prewitt = prewitt[:,:,0]
prewitt = prewitt.astype('int')
plt.imshow(prewitt,cmap='gray')

OUTPUT:

output| image gradient

Using Sobel filter

gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernelx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
kernely = np.array([[1, 2, 1],[0, 0, 0],[-1,-2,-1]])
img_x = cv2.filter2D(gray_img, -1, kernelx)
img_y = cv2.filter2D(gray_img, -1, kernely)

#taking root of squared sum and displaying result
new=np.hypot(img_x,img_y)
plt.imshow(new.astype('int'),cmap='gray')

OUTPUT:

output image gradient

Also Read: Image Processing Using OpenCV – With Practical Examples

Conclusion

This article taught us the basics of Image gradient and its application in edge detection. Image gradient is one of the fundamental building blocks of image processing. It is the directional change in the intensity of the image. The main application of the gradient of an image is in edge detection. Finding the change in intensity can conclude that it can be a boundary of an object. We can compute the gradient, its magnitude, and the orientation of the gradient manually. We usually use filters, which are of many kinds and for different results and purposes. The filters discussed in this article are the Roberts, Prewitt, and Sobel filters. We implemented the code in OpenCV, using all three filters to compute the gradient and eventually find the edges.

Some key points to be noted:

  • Image gradient is the building block of any edge detection algorithm.
  • We can manually find the image gradient and its strength and orientation.
  • We learned how to find the gradient and detect edges using different filters coded using OpenCV.

Frequently Asked Questions

Q1. What is a gradient in an image?

A. A gradient in an image refers to the change in intensity or colour between neighbouring pixels. It represents how quickly pixel values are transitioning from one shade or colour to another, providing a way to detect edges, textures, and other features within the image.

Q2. What is edge detection and image gradients?

A. Edge detection is the process of identifying significant transitions in intensity within an image, typically at object boundaries. Image gradients are used in edge detection, measuring the rate of change in pixel values and highlighting regions where these transitions occur.

Q3. What is the gradient of an image in CSS?

A. In CSS, an image gradient is a smooth transition between two or more colours. It creates visually appealing backgrounds and effects, commonly defined using linear-gradient or radial-gradient properties, allowing developers to control the direction and pattern of the gradient.

Q4. How to compute the gradient of an image?

A. Techniques like the Sobel or Prewitt operator are used to compute an image’s gradient. These methods calculate partial derivatives (in x and y directions) to measure changes in intensity. The gradient magnitude and direction can be derived from these calculations, which is useful in edge detection.

Q5. What is the gradient of a pixel image?

A. The gradient of a pixel in an image represents the direction and magnitude of intensity change around that pixel. Analyzing the differences in surrounding pixel values identifies the strength and orientation of transitions, helping detect edges, textures, and other features within the image.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details