Blogs/Sobel Edge Detection

Sobel Edge Detection

mesakarghm May 21 2021 1 min read 41 views
Computer Vision
sobel-edge-detection.jpeg

Edge Detection includes a variety of mathematical methods that helps us to identify points in an image where the image has some sort of discontinuities. Edge Detection is an important tool which is used extensively for feature detection and feature extraction in the field of digital image processing. 

Sobel Edge Detectior  is a edge detection implementation used in image processing where it creates an image emphasising edges. In Sobel Edge Detection, the image is processed in the X and Y directions seperately which is later combined together to form a new image. The new array formed after combination of the first two arrays represents the sum of the X and Y edges of the image. If A is the original image,  the following operation can be used for sobel edge detection in the X-axis. 

\(G_x = 1/4\begin{bmatrix} -1 & 0 & 1\\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix} * A \)

Similarly, we can compute it in the Y-direction as: 

\(G_y = 1/4 \begin{bmatrix} 1 & 1& 1\\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{bmatrix} * A\)

Here Gx and Gy represent the vertical and horizontal derrivative approximations of the input Image respectively. We can combine the gradient approximation to give the gradient magnitude using: 

\(G = \sqrt{G_x ^{2} + G_y^{2} } \)

Below I provide an implementation of the Sobel Edge Detection in Python. The two images show the effects on image before and after the application of Sobel Edge Detection. 

 

Learn and practice this concept here: 

https://mlpro.io/problems/edge_detection/

def edge_detection(image): 
    kernel1 = np.array([[-1/4,0,1/4],[-1/2,0,1/2],[-1/4,0,1/4]])
    kernel2 = np.array([[1/4,1/4,1/4],[0,0,0],[-1/4,-1/2,-1/4]])
        # Flip the kernel
    kernel1 = np.flipud(np.fliplr(kernel1))
    kernel2 = np.flipud(np.fliplr(kernel2))
    # convolution output
    output1 = np.zeros_like(image)
    output2 = np.zeros_like(image)

    # Add zero padding to the input image
    image_padded = np.zeros((image.shape[0] + 2, image.shape[1] + 2))
    image_padded[1:-1, 1:-1] = image

    # Loop over every pixel of the image
    for x in range(image.shape[1]):
        for y in range(image.shape[0]):
            # element-wise multiplication of the kernel and the image
            output1[y, x]=(kernel1 * image_padded[y: y+3, x: x+3]).sum()
            output2[y, x]=(kernel2 * image_padded[y: y+3, x: x+3]).sum()

    g = np.sqrt(output1 * output1 + output2 * output2)
    g *= 255.0 / np.max(g)
    return np.asarray(g,dtype = 'uint8')