Edge detection using Prewitt, Scharr and Sobel Operator


In digital image processing, the technique which is used to highlight the borders/boundaries of object withing a digital image is referred to as edge detection. We can use MATLAB to perform this operation. For this, there are several different built-in functions and operators are defined in the MATLAB library.

In this tutorial, I will explain three such operators namely, "Prewitt Operator", "Scharr Operator", and "Sobel Operator". All these three operators are commonly used gradient-based edge detection operators available in the MATLAB library.

Before going to discuss these operators for edge detection, let us first get a brief overview of edge detection technique.

What is Edge Detection?

Edge detection is a process in digital image processing which is used to detect the boundaries of objects within a digital image.

  • In edge detection process, the regions where rapid intensity change occurs within an image are highlighted.

  • In the field of image processing, edge detection is important because it is helpful in object recognition, image segmentation, extracting important features in an image, image quality enhancement, image compression, and more.

  • It is commonly used in various fields such as quality control in industrial applications, medical imaging to detect tumor, object detection in computer vision such as self−driving cars, robots, etc. remote sensing, image editing, and more.

Now, let us discuss how to use the "Prewitt operator", "Scharr operator", and "Sobel operator" to perform edge detection in MATLAB.

Edge Detection using Prewitt Operator in MATLAB

The Prewitt operator is a gradient−based edge detection operator used to detect boundaries of objects within a digital image. To detect and highlight the edges, the Prewitt operator approximates the gradient of the image intensity at each pixel.

Technically, the Prewitt operator utilizes two 3×3 convolution kernels to detect edges. One is used to detect the horizontal edges while the other one is to detect the vertical edges of objects within the image. These kernels are given below.

Horizontal Kernel

-1  0  1
-1  0  1
-1  0  1

Vertical Kernel

0  0  0
1  1  1

These Prewitt kernels are convolved with the image to calculate the gradient intensity at each pixel. Where, the horizontal kernel computes the changes in the gradient intensity from left to right, i.e., along the horizontal direction. Whereas, the vertical kernel computes the variations in the gradient intensity from top to bottom i.e., along the vertical direction. Finally, the two gradient images are combined to get the resulting image with highlighted edges.

Prewitt Operator Syntax

In MATLAB, to apply the Prewitt operator to detect edges in an image, the 'imfilter' function is used. It will have the following syntax:

out_img = imfilter(in_img, prewitt_kernel);

Here, "in_img" is the input image in which edge detection is to be performed. The "kernel" is the Prewitt operator kernel.

Example (1)

The following example demonstrates the implementation of the Prewitt operator to detect edges within an image.

% MATLAB code to edge detection using Prewitt operator
% Load the input image
in_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert input image to grayscale
gray_img = rgb2gray(in_img);

% Convert the image to double precision for calculation
gray_img = double(gray_img);

% Specify the Prewitt operator kernels
h_kernel = [-1, 0, 1; -1, 0, 1; -1, 0, 1];	% Horizontal Kernel
v_kernel = [-1, -1, -1; 0, 0, 0; 1, 1, 1];	% Vertical Kernel

% Use the Prewitt operator kernels to calculate gradient intensities
h_gradient = imfilter(gray_img, h_kernel);
v_gradient = imfilter(gray_img, v_kernel);

% Calculate the gradient magnitude
gradient_magnitude = sqrt(h_gradient.^2 + v_gradient.^2);

% Display the orignal and edge detected images
figure;
subplot(2, 1, 1);
imshow(in_img);
title('Original Image');

subplot(2, 1, 2);
imshow(uint8(gradient_magnitude));
title('Prewitt Edge Detected Image');

Output

Code Explanation

In this MATLAB example, we start by reading the input image and then convert it into grayscale and double precision for computation. After that, we define the horizontal and vertical Prewitt kernels and use the 'imfilter' function to apply Prewitt operator to the input image for edge detection.

Next, we compute the gradient magnitude which is basically visualization of detected edges of objects within the image. Finally, we use the "imshow" function to display the original and edge detected images.

Edge Detection using Scharr Operator in MATLAB

Similar to the Prewitt operator, the Scharr operator is another edge detection operator used in MATLAB to detect boundaries of objects within an image.

This operator is specially used where we have to detect diagonal edges, as it has more rotationally symmetric edge detection characteristics. Hence, the Scharr operator provides relatively better results in edge detection.

However, this is also a gradient-base edge detection operator and utilizes two 3×3 convolution kernels to approximate the gradient intensity at each pixel of an image. Here, one Scharr kernel is used to detect the horizontal edges, while the other is used to detect the vertical edges. These Scharr kernels are given below.

Horizontal Scharr Kernel

-3  0  3
-10  0  10
-3  0  3

Vertical Scharr Kernel

-3  -10  -3
0  0  0
3  10  3

These two kernels are convolved with the image to detect the edges of objects within the input image.

Scharr Operator Syntax

In MATLAB, the Scharr operator is applied using the "imfilter" function which has the following syntax,

out_img = imfilter(in_img, scharr_kernel);

Now, let us understand how to implement code in MATLAB to perform edge detection using the Scharr operator.

Example (2)

% MATLAB code to perform edge detection using Scharr operator
% Load the input image
in_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale
gray_img = rgb2gray(in_img);

% Convert the image to double precision for better calculation
gray_img = double(gray_img);

% Specify the Scharr operator kernels
h_kernel = [-3, 0, 3; -10, 0, 10; -3, 0, 3];	% Horizontal kernel
v_kernel = [-3, -10, -3; 0, 0, 0; 3, 10, 3];	% Vertical kernel

% Use the Scharr operator kernels to calculate gradients in image
h_gradient = imfilter(gray_img, h_kernel);
v_gradient = imfilter(gray_img, v_kernel);

% Calculate the gradient magnitude
gradient_magnitude = sqrt(h_gradient.^2 + v_gradient.^2);

% Display the original image and the edge detected image
figure;
subplot(2, 1, 1);
imshow(in_img);
title('Original Image');

subplot(2, 1, 2);
imshow(uint8(gradient_magnitude));
title('Scharr Edge Detected Image');

Output

Code Explanation

The implementation of this code is similar to that of the code of Prewitt operator. The only difference is that in this example, the kernel used for edge detection is Scharr operator kernel. From the output image, it is clear that the Scharr operator provides better results.

Let us now understand how the Sobel operator is used to detect edges of objects within an image.

Edge Detection using Sobel Operator in MATLAB

Similar to Prewitt and Scharr operators, the Sobel operator is also a gradient based edge detection operator used in digital image processing to detect boundaries of objects within an image. This operator also uses two 3×3 convolution kernels namely, horizontal Sobel kernel and vertical Sobel kernel. These Sobel kernels are given below.

Horizontal Sobel Kernel

-1  0  1
-2  0  2
-1  0  1

Vertical Sobel Kernel

-1 -2 -1
 0  0  0
 1  2  1

The process of detecting edges using the Sobel operator is similar to that for the Prewitt and Scharr operators.

Sobel Operator Syntax

In MATLAB, the Sobel operator is applied using the "imfilter" function. For this, the following syntax is used,

out_img = imfilter(in_img, sobel_kernel);

Let us now take an example to understand the use of Sobel operator to detect edges in an image.

Example (3)

% MATLAB code to detect edges using Sobel operator
% Load the input image
in_img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale
gray_img = rgb2gray(in_img);

% Convert the grayscale image to double precision
gray_img = double(gray_img);

% Specify the Sobel operator kernels
h_kernel = [-1, 0, 1; -2, 0, 2; -1, 0, 1];	% Horizontal kernel
v_kernel = [-1, -2, -1; 0, 0, 0; 1, 2, 1];	% Vertical kernel

% Use the Sobel operator kernels to calculate gradients
h_gradient = imfilter(gray_img, h_kernel);
v_gradient = imfilter(gray_img, v_kernel);

% Calculate the gradient magnitude
gradient_magnitude = sqrt(h_gradient.^2 + v_gradient.^2);

% Display the original image and the edge detected image
figure;
subplot(2, 1, 1);
imshow(in_img);
title('Original Image');

subplot(2, 1, 2);
imshow(uint8(gradient_magnitude));
title('Sobel Edge Detected Image');

Output

Code explanation

The code implementation and execution are similar to that of the above two examples. Here, we have used only Sobel operator kernel to detect edges.

Conclusion

In this tutorial, I have explained all these operators with the help of example programs. In conclusion, the Prewitt, Scharr, and the Sobel operators are used to perform edge detection of objects within a digital image using MATLAB.

Updated on: 10-Oct-2023

533 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements