Python Program for Determining Image Edges with OpenCV | Sobel edge detection method



Steps to download requirements below:

  • Run the following command on your terminal to install it from Ubuntu or Debian repository.
     sudo apt-get install libopencv-dev python-opencv 
  • OR To download OpenCV from the official site, run the following command:
     bash install-opencv.sh 

    on your terminal.

  • Enter your sudo password and you will have OpenCV installed.

Edge detection principle

Edge detection involves mathematical methods to find the points in an image where the brightness of the pixels changes distinctly.

  • The first thing we`re going to do is find the gradient grayscale images, which allows us to find edge-like areas in the x and y directions. The gradient is a multiparameter generalization of the derivative. While the derivative can be defined over functions of one variable, for functions of several variables, the gradient takes place.
  • The gradient is a vector function, not the derivative, which is scalar. Like the derivative, the gradient represents the slope of the tangent of the function plot . More precisely, the gradient points in the direction of the fastest increase in the function, and its value is — this is the slope of the graph in that direction.

Note. In computer vision, the transition from black to white is considered a positive slope, and the transition from white to black — negative.

# Python program to detect Edge
# using OpenCV in Python
# using Sobel edge detection
# and Laplacian method

import cv2

import numpy as np

 
# Capture live video from camera 0

cap = cv2.VideoCapture ( 0 )

  

while ( 1 ):

 

  # Take every frame

_ , frame = cap.read ()

 

  # Convert to HSV for easier calculations

hsv = cv2.cvtColor (frame, cv2.COLOR_BGR2HSV)

 

# Sobelx calculation

sobelx = cv2.Sobel (frame, cv2.CV_64F, 1 , 0 , ksize = 5 )

  

# Sobel`s calculation

sobely = cv2.Sobel (frame, cv2.CV_64F, 0 , 1 , ksize = 5 )

 

# Laplacian calculation

laplacian = cv2.Laplacian (frame, cv2.CV_64F)

 

cv2 .imshow ( `sobelx` , sobelx)

  cv2.imshow ( ` sobely` , sobely)

cv2.imshow ( `laplacian` , laplacian)

k = cv2.waitKey ( 5 ) & amp;  0xFF

if k = = 27 :

break

 
cv2.destroyAllWindows ()

 
# release the border
cap.release ()

Derived image calculation

A digital image is represented by a matrix that stores the RGB / BGR / HSV value (which color space the image belongs to) of each pixel in rows and columns. 
The derivative of the matrix is ​​calculated by an operator called the Laplacian . To compute the Laplacian, you will need to compute the first two derivatives, called Sobel derivatives, each of which accounts for changes in the gradient in a particular direction: one horizontal, the other vertical.

  • Horizontal Sobel x : obtained by convolution of the image with a matrix called the kernel, which is always odd in size. Kernel with size 3 — the simplest case.
  • Vertical Sobel y : obtained by convolution of the image with a matrix called the kernel, which is always odd in size. Kernel with size 3 — the simplest case.
  • Convolution is calculated using the following method: the image is the original image matrix, and the filter — kernel matrix.


    Ratio = 11 — 2- 2- 2- 2- 2 = 3
    Offset = 0

    Weighted sum = 124 * 0 + 19 * (- 2) + 110 * (- 2) + 53 * 11 + 44 * (- 2) + 19 * 0 + 60 * (- 2) + 100 * 0 = 117
    O [4,2] = (117/3) + 0 = 39

    So at the end, to get the Laplacian (approximation), we will need to combine the two previous results (Sobelx and Sobely) and save it in Laplacians.

Parameters :

  • cv2.Sobel (): function cv2.Sobel (frame, cv2.CV_64F, 1.0, ksize = 5) can be written as
     cv2.Sobel (original_image, ddepth, xorder, yorder, kernelsize) 

    where the first parameter is — original image, second parameter — depth of the target image. When ddepth = -1 / CV_64F, the destination image will have the same depth as the source. The third parameter — this is the order of the derivative of x. The fourth parameter — this is the order of the derivative y. When calculating Sobelx, we will set xorder to 1 and yorder to 0, while when calculating Sobly, the case will be the opposite. The last parameter — this is the size of the extended Sobel kernel; it should be 1, 3, 5 or 7.

  • cv2.Laplacian : in function
     cv2.Laplacian (frame, cv2.CV_64F)  

    first parameter — the original image, and the second parameter — depth of the target image. When depth = -1 / CV_64F, the final image will have the same depth as the original image.

Applications for edge detection

  • Reduce unnecessary information in the image while maintaining the structure of the image.
  • Extraction of important image features such as curves, corners and lines.
  • Recognizes objects, boundaries and segmentation.
  • Plays an important role in computer vision and recognition

Related article: