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.
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.
Matlab code for horizontal edges:
Matlab code for principal diagonal edges:
Matlab Code for secondary diagonal edges:
Final Matlab code for edge detection: