Edge Detection with 15 lines of python code using OpenCV and Webcam

Akash Bhiwgade
3 min readOct 9, 2019

--

Edge Detection with 15 lines of python code using OpenCV and Webcam

Welcome to the exciting world of Computer vision where there are no boundaries when it comes to exploring.

In this article we will discuss on how to detect edges in a video feed. For the learning purposes, we will use the webcam for a live video feed which can be surely be replaced by an image or any video file.

Let’s get this working.

Pre-requisites:

  1. OpenCV

I am using Python version 3.7 to write our script for edge detection. Let us discuss the steps to accomplish the task.

Steps:

  1. Turn on webcam and read the video feed
  2. Display the read video feed on a display window
  3. Detect edges and display it on the display window

And those three magical words, “Let us code”.

import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blur, 10, 70)
ret, mask = cv2.threshold(canny, 70, 255, cv2.THRESH_BINARY)
cv2.imshow('Video feed', mask)

if cv2.waitKey(1) == 13:
break
cap.release()
cv2.destroyAllWindows()

Let us break down the code for better understanding.

import cv2
cap = cv2.VideoCapture(0)

FIrst, we import the computer vision library and then turn on the primary webcam by using VideoCapture function.
I have passed 0 to this function as I am using my primary webcam. You can pass 1 if you have a secondary camera.

while True:
ret, img = cap.read()

Run through an infinite loop so as to keep the video feed live as per what the webcam reads.
Keep in mind that the infinite loop will need a break condition.
Inside the loop we use the read function and save the live video feed in img variable.

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blur, 10, 70)
ret, mask = cv2.threshold(canny, 70, 255, cv2.THRESH_BINARY)

Here, first we convert the image which has been read to grayscale using the cvtColor function so as to get better insights and analysis.
Second, convert the grayscale image to a blur image using GaussianBlur function which can then be used to detect edges on a feed.
Third, detect the edges using Canny function and pass this to the threshold function which converts the feed to binary (black and white, where background will be black and white will be the edges).

    cv2.imshow('Video feed', mask)

if cv2.waitKey(1) == 13:
break

Finally, view the edges on a display window by using the imshow function.
Also, remember to add the break condition. I will use the Enter key to exit out of the loop, where 13 is the key code for Enter button.

cap.release()
cv2.destroyAllWindows()

Last but not the least, release the webcam that was turned on earlier and destroy the display window which was used to display the edges on a live video feed.

Thanks for visiting this article, I hope you enjoyed. Do share your feedback in the comments section below. Till the next time.

Best Regards,
Akash Bhiwgade

--

--