Edge detection using OpenCV in Python
In this tutorial, we will learn how to perform Edge Detection using our webcam video using OpenCV in Python. By the end of this tutorial, you will know how incredibly easy it is!
In computer vision programs, we need to make sure the algorithm can make out distinct objects from the input image or video. As a result, we need to reduce as much noise as we can to train our models. To do this, one of our options is to detect edges. Please visit the OpenCV documentation page to know more. To perform edge detection we will be using the Canny filter tool in OpenCV.
So, let’s get started!
Install OpenCV library
First, of all, you need to ensure you have OpenCV installed on your PC. If not, just type in the following command into your command line.
pip install opencv-python
Import the library
Once the installation is complete, import the library.
Create VideoCapture object
We then need to create a VideoCapture object to read the frames from the input ie. our webcam video. If you want to convert another input file already saved on your PC you can type its path instead of 0 in the parameter.
After this, we need a loop since the read() method only captures one frame at a time. So, to capture a continuous video, we have to loop instructions. The read() method, which is called on the VideoCapture object, returns two variables: ret and frame.
while True: ret, frame = vcapture.read()
Here, the ret is a boolean variable that indicates if frame capture is successful or not. So we check if the frame is captured first, then perform the conversion of the frame stored in the frame variable to the edge extracted frame.
if ret == True:
Once the ret variable becomes true, we can convert the image to grayscale using the cvtColor() method. The first parameter is the frame to be converted. And, the filter to be converted to is the second parameter. Thankfully, for grayscale, there is a predefined filter in cv2 called COLOR_BGR2GRAY.
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
As a result, we can proceed to extract the edges from the grayscale frame. For this, we will use the Canny filter tool, Canny(). The first parameter is the grayscale frame that we just obtained. The next two parameters are called the thresholds. Higher the thresholds, the cleaner will be the output. You can experiment with different threshold values and see what those frames look like.
edge = cv2.Canny(grayscale, 75, 125)
Display on the window
We can now output the edge extracted frame to the window. The first parameter gives the window name and the second parameter is the frame to be displayed.
cv2.imshow('Edge frame', edge)
The next line of code waits for the user to enter a certain character, for instance ‘q’, to reak out of the loop to quit the window.
if cv2.waitKey(20) == ord('q'): break
Finally, we need to release the video capture object and close all the windows.
It is time to run the program and see the output!
The output will look like the video you can see below:
Note the markings on the cat and the curtains can be clearly seen. You can add noise or clean the image more by adjusting the parameters to the Canny() method.
Complete Python Code for edge detection:
The complete and final code of this tutorial is given below:
import cv2 vcapture = cv2.VideoCapture(0) while True: ret, frame = vcapture.read() if ret == True: grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) edge = cv2.Canny(grayscale, 75, 125) cv2.imshow('Edge frame', edge) if cv2.waitKey(20) == ord('q'): break vcapture.release() cv2.destroyAllWindows()
I hope, you would also like to read the articles given below: