Video processing using OpenCV
Video processing is manipulating and analyzing each video sequence frame to extract information or apply visual effects.
OpenCV (Open Source Computer Vision Library) is an
Frame operations
In video processing, each frame is processed individually or in sequence. OpenCV provides methods to access, manipulate, and analyze individual frames, such as resizing, cropping, flipping, and rotating them. These operations help prepare frames for further analysis or enhance the visual presentation.
In video processing, each frame is processed individually or in sequence. OpenCV offers methods to access, manipulate, and analyze individual frames. These operations include resizing, cropping, flipping, rotating, etc. They are crucial for preparing frames for subsequent analysis or improving visual presentation.
Example
Here's an example code that demonstrates frame by frame video processing. The code takes a video as input, applies a flip transformation, detects edges, and finally displays the resulting video.
import cv2
def process_video(input_video_path, output_video_path):
# Open the video file
video_capture = cv2.VideoCapture(input_video_path)
# Get the video properties
fps = video_capture.get(cv2.CAP_PROP_FPS)
frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Create a VideoWriter object to save the processed video
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Specify the video codec
output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height), isColor=False)
# Process each frame in the video
while video_capture.isOpened():
ret, frame = video_capture.read()
if not ret:
break
# Flip the frame horizontally
flipped_frame = cv2.flip(frame, 1)
# Apply edge detection
gray_frame = cv2.cvtColor(flipped_frame, cv2.COLOR_BGR2GRAY)
edges_frame = cv2.Canny(gray_frame, 100, 200)
# Display the output frame
cv2.imshow("Processed Video", edges_frame)
# Write the processed frame to the output video file
output_video.write(edges_frame)
# Exit if the 'q' key is pressed
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Release the video capture and output video objects
video_capture.release()
output_video.release()
# Close all windows
cv2.destroyAllWindows()
# Example usage
input_video_path = "input.mp4"
output_video_path = "output.mp4"
process_video(input_video_path, output_video_path)Code explanation
Line 1: Imports
cv2library.Line 48: Stores input video (named
"input.mp4") ininput_video_pathvariable.Line 49: Stores output video (named
"output.mp4") inoutput_video_pathvariable.Line 51: Both video paths are passed to the
process_video()function.Line 5: Uses
VideoCapture()method ofcv2library to read the input video and store it invideo_capturevariable.Lines 8–10: These lines extract the input video's features.
get()method of captured video in line 5 returns the frames per second of the video to the variablefps. Theget()method is again used to get the frame's width and height and store them inframe_widthandframe_heightvariables, respectively.Line 13:
VideoWriter_fourcc()method sets the format of the output video to.mp4.Line 14:
VideoWriter()method sets the.mp4format, frames per second, dimensions, and greyscale mask tooutput_videoand saves it in theoutput_videovariable.Line 17: Starts a while loop and lets it run until every video frame has been traversed.
Line 18: The
read()method reads the processed input image and stores the frame in theframevariable. Theframevariable gets updates in each iteration.retis a boolean variable that storesTrueif aframehas been read successfully. Otherwise, it storesFalse.Lines 20–21: If a
framehas not successfully been read, it breaks the loop.Line 24: The
flip()method of thecv2library flips theframe. It stores the flipped frame in theflipped_framevariable.Line 28: Applies the canny operator to the flipped greyscaled frames.
Line 31: Displays each frame on the screen.
Line 34: Writes every frame over the
output_videowe defined in line 14.Line 37: Waits for the user to press
qif they want to quit.Lines 41–42: Saves the video with the greyscaled flipped frames.
Line 45: Closes every opened window.
Conclusion
Video processing is just image processing, but the only difference is that it applies to each video frame. There are a lot of other operations which can be applied to the videos, such as object tracking, background removal or filtering, etc.
Free Resources