Edge Detection –
Edge detection is a useful technique to detect the edges of surfaces and objects in the video. Edge detection involves the following steps:
- Noise reduction
- Gradient calculation
- Non-maximum suppression
- Double threshold
- Edge tracking by hysteresis
# importing the necessary libraries
import cv2
import numpy as np
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
# Loop until the end of the video
while (cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
interpolation = cv2.INTER_CUBIC)
# Display the resulting frame
cv2.imshow('Frame', frame)
# using cv2.Canny() for edge detection.
edge_detect = cv2.Canny(frame, 100, 200)
cv2.imshow('Edge detect', edge_detect)
# define q as the exit button
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# release the video capture object
cap.release()
# Closes all the windows currently opened.
cv2.destroyAllWindows()
Output:
Python – Process images of a video using OpenCV
Processing a video means, performing operations on the video frame by frame. Frames are nothing but just the particular instance of the video in a single point of time. We may have multiple frames even in a single second. Frames can be treated as similar to an image.
So, whatever operations we can perform on images can be performed on frames as well. Let us see some of the operations with examples.