Wednesday, February 21, 2018

Sobel-Edge-Detection

Sobel-Edge-Detection

If the reader does not know the convolution operation, click this link to understand the convolution operation in image processing: convolution.This article illustrates the how to implement Sobel edge detection without using predefined function. It is very simple to understand and implement. The function naveenSobelXgradient() calculates the horizontal derivative approximation and naveenSobelYgradient() calculates the vertical derivative approximation. Both these functions use the 3 x 3 kernels of Sobel edge detection as shown in following. The function naveenConvolve() do convolution operation between kernels and input image. After finding the X and Y gradients, the gradient magnitude is calculated using either [imgx2 + imgy2] or [abs(imgx) + abs(imgy)]. Here, imgx and imgy are X and Y gradients of given image img. We have used the latter one for calculating sobel edge detection. Python with openCV is used for reading the image file but sobel edge detection is done by the user defined function.

Sobel Kernels for Edge detection

Requirements for execution of code

  1. Python (numpy package)
  2. Opencv (Cv2 package)

Python Code

# Developer : M NAVEENKUMAR, a Research Scholar, Department of Computer Applications, NIT Trichy, Tamilnadu, India
# Objective of this program is to implement sobel edge detection without using Predefined functions.
import numpy as np
import cv2

#a function for convolution operation
def naveenConvolve(img,kernel):
    row1total = img[0,1]*kernel[0,1] + img[0,2]*kernel[0,2] + img[0,0]*kernel[0,0]
    row2total = 0
    row3total = img[2,1]*kernel[2,1] + img[2,2]*kernel[2,2] + img[2,0]*kernel[2,0]
    return row1total + row2total + row3total

#a function for taking part of image to apply convolution between kernel and part of image
def takePartImage(inpimg,i,j):
    image = np.zeros((3,3))
    a = i
    b = j
    for k in range(0,3):
        b = j
        for l in range(0,3):
            image[k,l] = inpimg[a,b]
            b = b+1
        a = a +1
    return image

#a function for finding X gradient
def naveenSobelXgradient(inputimg):
    rows = len(inputimg)
    cols = len(inputimg[0])
    Gx = np.array(np.mat('1 0 -1; 2 0 -2; 1 0 -1'))
    outputimg = np.zeros((rows,cols))
    for i in range(0,rows-3):
         for j in range(0,cols-3):
             # retreve the part of image of 3 x 3 dimension from inputimage
             image  = takePartImage (inputimg, i, j)
             outputimg[i,j] = naveenConvolve(image,Gx)
    return outputimg

#a function for finding Y gradient
def naveenSobelYgradient(inputimg):
    rows = len(inputimg)
    cols = len(inputimg[0])
    #print(rows,cols)
    Gy = np.array(np.mat('1 2 1; 0 0 0; -1 -2 -1'))
    outputimg = np.zeros((rows,cols))
    for i in range(0,rows-3):
         for j in range(0,cols-3):
             # retreve the part of image of 3 x 3 dimension from inputimage
             image  = takePartImage (inputimg, i, j)
             outputimg[i,j] = naveenConvolve(image,Gy)
    return outputimg

#reading an image 
inputimg = cv2.imread('Image path',0);

sobelimagex = naveenSobelXgradient(inputimg)
sobelimagey = naveenSobelYgradient(inputimg)

rows = len(inputimg)
cols = len(inputimg[0])

outputimg = np.zeros([rows, cols])

#finding the gradient magnitude by using the formula [abs(imgx) + abs(imgy)]
for i in range(0,rows):
    for j in range(0,cols):
        outputimg[i,j] = abs(sobelimagex[i, j]) + abs(sobelimagey[i, j])

print(outputimg.size)

cv2.imshow('sobel image',np.uint8(outputimg))
cv2.waitKey(0)

Output

Input image Sobel Output
Input image Sobel Output



Follow us on Facebook :



Popular Articles:

1. matlab-cropping-binary-image-algorithm

Objective of the Program: Program takes a black and white image as input. It removes the black portion and gives the white portion of the image.

2. Working-with-ROI-of-image-using-Matlab

Objective of the Program:The part of the image, on which you have interest to work out, is called Region of Interest (ROI). In another words, selected subset of image is called ROI. In some contexts, you want to apply operations on ROI of image rather than the entire image. To achieve this, generally people extract the ROI from the image, store it in another variable and then apply operations on ROI. If you want to apply your operations on ROI without extracting from the image, it is bit difficult. This article will explain the performing the operations on ROI without extracting from the image. In this context, the ROI part of image is affected rather than the entire image.

Python-environment-for-deep-learning-in-windows

Python is increasingly becoming a popular programming language for machine learning and deep learning. If you want to use python for train...