Edge Detection in MATLAB Using the Sobel Operator: A Step-by-Step Guide

NeuralNinja
2 min readApr 15, 2023

--

Introduction:

Image Gradient is a crucial concept in image processing because it allows us to recognize edges, contours, and other elements in an image. The gradient is the rate at which an image’s brightness or intensity changes, and it is computed by taking the image’s derivative. In this blog article, we will look at how to compute gradients without utilizing built-in functions using the Sobel operator.

Explanation of Code:

First, we use the imread() method to read an image (for the sake of this code gray version of image is used) and then compute its size. The image is then converted to double format since the Sobel operator works with double format. Using the zeros() method, we generate a padded image that is 2 pixels bigger on each side than the original image. This is done to execute convolution on the image’s edges as well.

The Sobel operator mask is then defined. The Sobel operator is a 3x3 filter that detects image edges. It computes the gradient in the x and y directions using two kernels, X and Y, respectively. The horizontal margins are highlighted by the X kernel, while the vertical edges are highlighted by the Y kernel.

Padding is then applied to the original image. Padding is required to avoid losing information at the image’s edges during convolution. Padding comes in a variety of forms, including zero-padding, symmetric padding, and reflection padding. We use zero-padding in this code to pad the image with zeros.

After padding, we cycle over the padded image with two nested loops then perform convolution using the Sobel operator. Using the X and Y kernels, we compute the gradient in the x and y directions, respectively.

The magnitude of the gradient is then calculated using the following equation:

mag_img = sqrt(x_direction.^² + y_direction.^²);

Finally, we can use the imshow() method to display the original image, x-direction gradient, y-direction gradient, and magnitude of the gradient.

Conclusion:

Finally, we explored how to calculate gradient without utilizing built-in functions using the Sobel operator. We also discussed the significance of padding in convolution and the various forms of padding that are routinely utilized. In image processing, the Sobel operator is a must-have tool for edge detection. The gradient computed by the Sobel operator may be employed in a variety of applications, including object identification, image segmentation, and feature extraction.

%gradient with sobel
original_img=imread('');
[r, c]=size(original_img);
double_img = double(original_img);
padded_img=zeros(size(double_img)+2);
% Sobel Operator Mask
X = [1 0 -1; 2 0 -2; 1 0 -1];
Y = [1 2 1; 0 0 0; -1 -2 -1];
%padding
for x=1:r
for y=1:c
padded_img(x+1,y+1)=original_img(x,y); %Padding the original matrix
end
end
for i = 1:size(padded_img, 1) - 2
for j = 1:size(padded_img, 2) - 2
% % Gradient approximations
Gx = sum(sum(X.*padded_img(i:i+2, j:j+2)));
Gy = sum(sum(Y.*padded_img(i:i+2, j:j+2)));
x_direction(i+1,j+1)=Gx;
y_direction(i+1,j+1)=Gy;
end
end
% Calculate magnitude
mag_img = sqrt(x_direction.^2 + y_direction.^2);
%subplot (2,2,1); imshow(original_img); title("original image");
%subplot (2,2,2); imshow(uint8(x_direction)); title("x-direction");
%subplot (2,2,3); imshow(uint8(y_direction)); title("y-direction");
%subplot (2,2,4); imshow(uint8(mag_img)); title("magnitude");

--

--

No responses yet