Matlab | Edge Detection of an image without using in-built function
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:

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:

Recommended Posts:
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- MATLAB | RGB image to grayscale image conversion
- Matlab | Erosion of an Image
- Image Complement in Matlab
- Image Resizing in Matlab
- MATLAB | RGB image representation
- Matlab | Dilation of an Image
- MATLAB | Complement colors in RGB Image
- LSB based Image steganography using MATLAB
- Extract bit planes from an Image in Matlab
- MATLAB | Complement colors in a Binary image
- Flip image across Secondary Diagonal in MATLAB
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



