Computer Vision: from Scratch: Ex-16, Image Gradient [Laplacian & Sobel] image processing Technique

Mr. kashyap
4 min readAug 25, 2023

--

Image gradient, Laplacian, and Sobel are concepts and techniques commonly used in image processing and computer vision for various tasks like edge detection, feature extraction, and image enhancement.

  1. Image Gradient: The gradient of an image represents the rate of change of pixel values across the image. It’s a vector that points in the direction of the steepest increase in intensity. In simpler terms, it tells us how the intensity of the image changes from one pixel to its neighboring pixels. Mathematically, for a grayscale image, the gradient in the x-direction (horizontal) and y-direction (vertical) can be computed using partial derivatives:
  • Horizontal Gradient (Gx) = Image(x+1, y) — Image(x-1, y)
  • Vertical Gradient (Gy) = Image(x, y+1) — Image(x, y-1)

The magnitude of the gradient can be calculated as:

  • Gradient Magnitude (G) = sqrt(Gx² + Gy²)

The direction of the gradient can be calculated as:

  • Gradient Direction (θ) = atan(Gy / Gx)

The gradient information helps in detecting edges in an image.

2. Laplacian: The Laplacian is a second-order derivative operator that measures the rate of change of the gradient magnitude. It helps to find regions of rapid intensity change in an image, which can correspond to edges or corners. The Laplacian operator is often used for edge detection and image sharpening. For a grayscale image, the Laplacian operator can be applied as follows:

  • Laplacian Image = Gx² + Gy²

In simple words, the Laplacian highlights areas where the intensity changes abruptly, which can indicate potential edge locations.

3. Sobel Operator: The Sobel operator is a widely used edge detection filter that approximates the gradient of an image. It is a convolutional filter that emphasizes edges by calculating the gradient magnitude in both the x and y directions. The Sobel operator can be used to find the edges’ strength and direction.

  1. The Sobel operator consists of two convolution masks, one for detecting edges in the horizontal direction (Gx) and another for detecting edges in the vertical direction (Gy). These masks highlight the changes in pixel intensity along the respective directions.
  2. The Sobel operator is often used in edge detection tasks because it’s relatively simple to compute and gives good results for many cases.
Basic example of Image Gradient

In OpenCV, you can use the functions cv2.Laplacian() and cv2.Sobel() to compute the image gradient, Laplacian, and Sobel derivatives. These operations are commonly used in image processing and computer vision for tasks such as edge detection and feature extraction.

Here is a simple code snippet for Image Gradient..

import cv2
import numpy as np

# Read the input image
image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Compute Laplacian gradient
laplacian = cv2.Laplacian(image, cv2.CV_64F)

# Compute Sobel gradients in X and Y directions
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) # Sobel X
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3) # Sobel Y

# Optional: Convert gradients to absolute values
laplacian_abs = cv2.convertScaleAbs(laplacian)
sobel_x_abs = cv2.convertScaleAbs(sobel_x)
sobel_y_abs = cv2.convertScaleAbs(sobel_y)

# Display the computed gradients
cv2.imshow('Laplacian', laplacian_abs)
cv2.imshow('Sobel X', sobel_x_abs)
cv2.imshow('Sobel Y', sobel_y_abs)

cv2.waitKey(0)
cv2.destroyAllWindows()

Another Code Snippet for the same:

# Image Gradient..
# It is a directional change in the color or intensity in an image
# it is most important part in finding information from range.
# Like finding edges within the images.
# There are various methods to find image gradient.
# laplacian Derivatives, SobelX and SobelY.
# image should be gray scale.

import cv2
import numpy as np

img = cv2.imread("C:\\Users\\RANGER\\Desktop\\pixabay.jpg",0)
img = cv2.resize(img,(400,400))

# Laplacian = it calculate la placian derivate in pixels.
# parameter (image, data_type for -ve val,ksize)
# why datatype (because in laplacian derivate calculation if any -ve number found then these special datatye will handle those -ve numbers.)
lap = cv2.Laplacian(img,cv2.CV_64F,ksize=3)
# here in edges filtration you may find so much noise.
# so for clear edge filtration we will create array
# ksize will alway be odd number.
lap = np.uint8(np.absolute(lap))


# sobel Operation
# joint operation of Gaussian Smoothening and Differentiation operation.
# more resistant to noise.
# parameter(image, type for -ve val, x= 1(#vertical lines), y=0(horizontal line), ksize)

sobelX= cv2.Sobel(img, cv2.CV_64F,1,0,ksize=3)
sobelY= cv2.Sobel(img, cv2.CV_64F,0,1,ksize=3)
# here you may find very noise and distortion which is caused by datatype which hold 64 byte int size.
# which is why we need to convert it into unsigned int 8.
sobelX = np.uint8(np.absolute(sobelX))
sobelY = np.uint8(np.absolute(sobelY))

# here you can find the difference i.e., in sobelX pixel will be verfied by vertically (top-buttom),
# but in sobelY it is verified by Horizontal way( left - right).

# finally combine sobelX and sobelY
sobelcombine = cv2.bitwise_or(sobelX,sobelY)

cv2.imshow("grayimage",img)
cv2.imshow("Laplacian",lap)
cv2.imshow("Sobelx",sobelX)
cv2.imshow("Sobely",sobelY)
cv2.imshow("SobelCombine",sobelcombine)
cv2.waitKey(0)
cv2.destroyAllWindows()

In summary, these concepts and techniques are fundamental in image processing and computer vision. The gradient provides information about intensity changes, the Laplacian helps to find regions of rapid intensity change, and the Sobel operator is a specific filter used for edge detection. These tools are building blocks for various image analysis tasks.

--

--