The following program detects the edges of frames in a livestream video content. The code will only compile in linux environment. Make sure that openCV is installed in your system before you run the program.
Steps to download the requirements below:
- Run the following command on your terminal to install it from the Ubuntu or Debian respository.
sudo apt-get install libopencv-dev python-opencv
- OR In order to download OpenCV from the official site run the following command:
on your terminal.
- Type your sudo password and you will have installed OpenCV.
Principle behind Edge Detection
Edge detection involves mathematical methods to find points in an image where the brightness of pixel intensities changes distinctly.
- The first thing we are going to do is find the gradient of the grayscale image, allowing us to find edge-like regions in the x and y direction. The gradient is a multi-variable generalization of the derivative. While a derivative can be defined on functions of a single variable, for functions of several variables, the gradient takes its place.
- The gradient is a vector-valued function, as opposed to a derivative, which is scalar-valued. Like the derivative, the gradient represents the slope of the tangent of the graph of the function. More precisely, the gradient points in the direction of the greatest rate of increase of the function, and its magnitude is the slope of the graph in that direction.
Note: In computer vision, transitioning from black-to-white is considered a positive slope, whereas a transition from white-to-black is a negative slope.
Calculation of the derivative of an image
A digital image is represented by a matrix that stores the RGB/BGR/HSV(whichever color space the image belongs to) value of each pixel in rows and columns.
The derivative of a matrix is calculated by an operator called the Laplacian. In order to calculate a Laplacian, you will need to calculate first two derivatives, called derivatives of Sobel, each of which takes into account the gradient variations in a certain direction: one horizontal, the other vertical.
- Horizontal Sobel derivative (Sobel x): It is obtained through the convolution of the image with a matrix called kernel which has always odd size. The kernel with size 3 is the simplest case.
- Vertical Sobel derivative (Sobel y): It is obtained through the convolution of the image with a matrix called kernel which has always odd size. The kernel with size 3 is the simplest case.
- Convolution is calculated by the following method: Image represents the original image matrix and filter is the kernel matrix.
Factor = 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 in the end to get the Laplacian (approximation) we will need to combine the two previous results (Sobelx and Sobely) and store it in laplacian.
- cv2.Sobel(): The function cv2.Sobel(frame,cv2.CV_64F,1,0,ksize=5) can be written as
where the first parameter is the original image, the second parameter is the depth of the destination image. When ddepth=-1/CV_64F, the destination image will have the same depth as the source. The third parameter is the order of the derivative x. The fourth parameter is the order of the derivative y. While calculating Sobelx we will set xorder as 1 and yorder as 0 whereas while calculating Sobely, the case will be reversed. The last parameter is the size of the extended Sobel kernel; it must be 1, 3, 5, or 7.
- cv2.Laplacian: In the function
the first parameter is the original image and the second parameter is the depth of the destination image.When depth=-1/CV_64F, the destination image will have the same depth as the source.
Edge Detection Applications
- Reduce unnecessary information in an image while preserving the structure of image.
- Extract important features of image like curves, corners and lines.
- Recognizes objects, boundaries and segmentation.
- Plays a major role in computer vision and recognition
Related Article: Edge Detection using Canny edge detection method
This article is contributed by Pratima Upadhyay. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- MATLAB - Image Edge Detection using Sobel Operator from Scratch
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Python | Corner detection with Harris Corner Detection method using OpenCV
- Python | Corner Detection with Shi-Tomasi Corner Detection Method using OpenCV
- Image Edge Detection Operators in Digital Image Processing
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Matlab | Edge Detection of an image without using in-built function
- MATLAB - Image Edge Detection using Prewitt Operator from Scratch
- MATLAB - Image Edge Detection using Robert Operator from Scratch
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Python | Detect corner of an image using OpenCV
- Python | Detect Polygons in an Image using OpenCV
- Python - Edge Detection using Pillow
- Edge detection using in-built function in MATLAB
- OpenCV Python program for Vehicle detection in a Video frame
- Opencv Python program for Face Detection
- Implement Canny Edge Detector in Python using OpenCV
- Detection of a specific color(blue here) using OpenCV with Python
- Face Detection using Python and OpenCV with webcam
- Python | Smile detection using OpenCV