Edge detection using first derivative operator in MATLAB


In digital image processing, edge detection is a process used to identify the boundaries of objects within a digital image. We have various image processing techniques to detect edges in an image, but in this tutorial, we will learn the edge detection using first derivative operator in MATLAB.

What is Edge Detection in MATLAB?

MATLAB is an efficient tool to perform complex image processing tasks. One such task is the edge detection, which is nothing but a process of detecting boundaries of objects in an image.

  • In an image, an edge of an object is typically the region where sudden intensity change occurs.

  • Edge detection is a crucial process because it plays a vital role in various fields of engineering and technology, such as image segmentation, features extraction, image quality enhancement, self−driving cars and robotics, and more.

Now, let us discuss the process of edge detection using first derivative operator in MATLAB.

Edge Detection using First Derivative Operator in MATLAB

In MATLAB, we can follow the steps given below to perform edge detection using first derivative operator:

Step (1) − Read the input image using the "imread" function. For this, use the following syntax:

img = imread('Image.jpg');

Step (2) − Convert the input image to gray scale to process using digital tools like MATLAB. For this use the "rgb2gray" function.

gray_img = rgb2gray(img);

Step (3) − Convert the grayscale image to double precision for calculation. Do this as follows:

gray_img = double(gray_img);

Step (4) − Define the first derivative operator ‘D’ to perform edge detection. It can be a "forward operator", "central operator", or "backward operator".

Step (5) − Convolve the image with the first derivative operator to detect the edges of objects within the image. For this use the "conv2" function as follows:

edge_img = conv2(gray_img, D);

Step (6) − Adjust the edge detected image so that it will be suitable to visualize. For this, you can use the "abs" function to take absolute value of the convolved image and then can convert the resulting image to 8−bit unsigned integer format to display. Do this as follows:

edge_img = abs(edge_img);
edge_img = uint8(edge_img);

Step (7) − Finally, display the resulting edge detected image using the ‘imshow’ function as follows:

imshow(edge_img);

Now, let us consider some example programs in MATLAB to practically understand how to perform edge detection using first derivative operator.

Example (1) − Edge Detection using Forward Operator

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

% Convert the input image to grayscale for processing
gray_img = rgb2gray(img);

% Convert the grayscale image to double data type for calculation
gray_img = double(gray_img);

% Create the first derivative operator
D = [1 -1 0];	% Forward operator

% Convolve the image with forward operator to detect edges
Cx = conv2(gray_img, D, 'same');	% Convolve along x-axis
Cy = conv2(gray_img, D, 'same');	%Convolve along y-axis

% Compute the edge magnitude (edge image)
edge_img = sqrt(Cx.^2 + Cy.^2);

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

subplot(2, 1, 2);
imshow(edge_img, []);
title('Edge Image');

Output

It will produce the following output image:

Code Explanation

In this MATLAB code, we start by reading the input image using the "imread" function. Then, we convert the input image from RGB color scale to gray scale for processing, and also convert it to double data type for calculations.

After that, define the forward first derivative operator "D" and convolve the gray image along the x−axis and y−axis with this operator using the "conv2" function to detect the edges. After that we compute the edge magnitude to get the full edge detected image.

Finally, we display the original image and the edge detected image using the "imshow" function.

Example (2) − Edge Detection using Central Operator

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

% Convert the input image to grayscale for processing
gray_img = rgb2gray(img);

% Convert the grayscale image to double data type for calculation
gray_img = double(gray_img);

% Create the first derivative operator
D = [1 0 -1];	% Central operator

% Convolve the image with forward operator to detect edges
Cx = conv2(gray_img, D, 'same');	% Convolve along x-axis
Cy = conv2(gray_img, D, 'same');	%Convolve along y-axis

% Compute the edge magnitude (edge image)
edge_img = sqrt(Cx.^2 + Cy.^2);

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

subplot(2, 1, 2);
imshow(edge_img, []);
title('Edge Image');

Output

It will produce the following output image:

Code Explanation: The implementation and execution of this MATLAB code is the same as that of the previous one. The only difference is that here the edge detection is done with first derivative central operator.

Example (3) − Edge Detection using Backward Operator

% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Convert the input image to grayscale for processing
gray_img = rgb2gray(img);

% Convert the grayscale image to double data type for calculation
gray_img = double(gray_img);

% Create the first derivative operator
D = [0 1 -1];	% Backward operator

% Convolve the image with forward operator to detect edges
Cx = conv2(gray_img, D, 'same');	% Convolve along x-axis
Cy = conv2(gray_img, D, 'same');	%Convolve along y-axis

% Compute the edge magnitude (edge image)
edge_img = sqrt(Cx.^2 + Cy.^2);

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

subplot(2, 1, 2);
imshow(edge_img, []);
title('Edge Image');

Output

It will produce the following output image:

Code Explanation: The implementation and execution of this MATLAB code is same as that of the previous two. The only difference is that here the edge detection is performed using the first derivative backward operator.

Conclusion

In conclusion, edge detection is a process of highlighting the boundaries of objects within an image. MATLAB can be used to perform edge detection. In this tutorial, I have explained the process of edge detection using the first derivative operator with the help of example programs in MATLAB. You can try all these codes with your own images.

Updated on: 10-Oct-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements