Problem Statement: Write a matlab code for edge detection of a grayscale image without using in-built function of edge detection.
About Edge Detection:
Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness. Edge detection is used for image segmentation and data extraction in areas such as image processing, computer vision, and machine vision.
Approach:
For edge detection, we take the help of convolution: Convolution = I * m where I is the image, m is the mask and * is convolutional operator. To perform convolution on an image following steps are required:
- Flip the mask horizontally and then vertically. This will result in 180-degree rotation of an image.
- Slide the mask onto the image such that every pixel in the image coincides with the center of the mask atleast once.
- Multiply the corresponding elements with the pixel values below it and then add them.
- Repeat this procedure until all pixel values of the image have been calculated for updation.
Now, we will take 3×3 mask for the same.
3×3 Mask for vertical edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
3×3 Mask for horizontal edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
3×3 Mask for principal diagonal edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
3×3 Mask for secondary diagonal edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
We’ll find these edges separately and finally combine them using max
function or mean function, but max
function is more accurate for this.
Original image:

Matlab Code for vertical edges:
I = double((imread( 'image1.jpg' )); % read image
In = I; % copy image
mask = [ 1 , 0 , - 1 ; 1 , 0 , - 1 ; 1 , 0 , - 1 ];
% Rotate image by 180 degree first flip up to down then left to right
mask = flipud(mask);
mask = fliplr(mask);
for i = 2 :size(I, 1 ) - 1
for j = 2 :size(I, 2 ) - 1
% multiplying mask value with the corresponding image pixel value
neighbour_matrix = mask. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value = sum (neighbour_matrix(:));
I(i, j) = avg_value;
end
end
figure, imshow(uint8(I));
|
Output:

Matlab code for horizontal edges:
I = double((imread( 'image1.jpg' ));
In = I;
mask = [ 1 , 1 , 1 ; 0 , 0 , 0 ; - 1 , - 1 , - 1 ];
mask = flipud(mask);
mask = fliplr(mask);
for i = 2 :size(I, 1 ) - 1
for j = 2 :size(I, 2 ) - 1
neighbour_matrix = mask. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value = sum (neighbour_matrix(:));
I(i, j) = avg_value;
end
end
figure, imshow(uint8(I));
|
Output:

Matlab code for principal diagonal edges:
I = double((imread( 'image1.jpg' ));
In = I;
mask = [ 0 , - 1 , - 1 ; 1 , 0 , - 1 ; 1 , 1 , 0 ];
mask = flipud(mask);
mask = fliplr(mask);
for i = 2 :size(I, 1 ) - 1
for j = 2 :size(I, 2 ) - 1
neighbour_matrix = mask. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value = sum (neighbour_matrix(:));
I(i, j) = avg_value;
end
end
figure, imshow(uint8(I));
|
Output:

Matlab Code for secondary diagonal edges:
I = double((imread( 'image1.jpg' ));
In = I;
mask = [ 1 , 1 , 1 ; 0 , 0 , 0 ; - 1 , - 1 , - 1 ];
mask = flipud(mask);
mask = fliplr(mask);
for i = 2 :size(I, 1 ) - 1
for j = 2 :size(I, 2 ) - 1
neighbour_matrix = mask. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value = sum (neighbour_matrix(:));
I(i, j) = avg_value;
end
end
figure, imshow(uint8(I));
|
Output:

Final Matlab code for edge detection:
I = double(imread( 'image1.jpg' ));
In = I;
mask1 = [ 1 , 0 , - 1 ; 1 , 0 , - 1 ; 1 , 0 , - 1 ];
mask2 = [ 1 , 1 , 1 ; 0 , 0 , 0 ; - 1 , - 1 , - 1 ];
mask3 = [ 0 , - 1 , - 1 ; 1 , 0 , - 1 ; 1 , 1 , 0 ];
mask4 = [ 1 , 1 , 0 ; 1 , 0 , - 1 ; 0 , - 1 , - 1 ];
mask1 = flipud(mask1);
mask1 = fliplr(mask1);
mask2 = flipud(mask2);
mask2 = fliplr(mask2);
mask3 = flipud(mask3);
mask3 = fliplr(mask3);
mask4 = flipud(mask4);
mask4 = fliplr(mask4);
for i = 2 :size(I, 1 ) - 1
for j = 2 :size(I, 2 ) - 1
neighbour_matrix1 = mask1. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value1 = sum (neighbour_matrix1(:));
neighbour_matrix2 = mask2. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value2 = sum (neighbour_matrix2(:));
neighbour_matrix3 = mask3. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value3 = sum (neighbour_matrix3(:));
neighbour_matrix4 = mask4. * In(i - 1 :i + 1 , j - 1 :j + 1 );
avg_value4 = sum (neighbour_matrix4(:));
% using max function for detection of final edges
I(i, j) = max ([avg_value1, avg_value2, avg_value3, avg_value4]);
end
end
figure, imshow(uint8(I));
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Jun, 2019
Like Article
Save Article