Coders Packet

Sobel edge detection from scratch using Python

By Vedant Keshav Jadhav

Edge detection is a very important image processing technique. In this packet, we will take a look at the algorithm of Sobel edge detection in Python, which uses the Sobel operator.

Edge Detection, as the name suggests, is a process of detecting sharp changes in intensities of an image, by using matrix mathematics. Areas where there are extreme differences, that is, either a sharp rise or fall in the intensities of the pixel usually indicate an edge of an object. After finding all of the large differences in intensities in a picture, we have discovered all of the edges in the picture. Sobel Edge detection is one of the most basic image processing algorithms and is widely used.

The main crux of this algorithm is the Sobel operator. It is an approximated 3x3 gradient operator, or kernel (also called filter), which is convolved with the original image. The kernel is different for both the X and Y directions. The image is processed in the X and Y directions separately, using the X and Y gradient matrix first, and then combined together to form a new image which represents the sum of the X and Y edges of the image.

The X kernel, when convolved with images, detects the horizontal edges, whereas the Y kernel detects verticle edges


Implementation -

1. First we define both the gradients as a 2D array.

2. We convert the RGB into a monochrome image. The most standard monochrome image is the grayscale image.

3. Next we run the kernels through images and convolve them. Before convolution, we pad the images to retain the original size of the image.

4. Next we take the vectorial sum of the gradient value.


Dependencies -

1. Matplotlib

2. Numpy

3. Math module

4. Time module (for saving purposes, not necessary)



Input image -

Output image -

Download Complete Code


No comments yet