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 open-sourceA software or a library whose code is made available to the public. machine learning library widely used for computer vision and video processing tasks.

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)
Python code to flip a video and detect edges in it

Code explanation

  • Line 1: Imports cv2 library.

  • Line 48: Stores input video (named "input.mp4") in input_video_path variable.

  • Line 49: Stores output video (named "output.mp4") in output_video_path variable.

  • Line 51: Both video paths are passed to the process_video() function.

  • Line 5: Uses VideoCapture() method of cv2 library to read the input video and store it in video_capture variable.

  • 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 variable fps. The get() method is again used to get the frame's width and height and store them in frame_width and frame_height variables, respectively.

  • Line 13: VideoWriter_fourcc() method sets the format of the output video to .mp4.

  • Line 14: VideoWriter() method sets the .mp4 format, frames per second, dimensions, and greyscale mask to output_video and saves it in the output_video variable.

  • 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 the frame variable. The frame variable gets updates in each iteration. ret is a boolean variable that stores True if a frame has been read successfully. Otherwise, it stores False.

  • Lines 20–21: If a frame has not successfully been read, it breaks the loop.

  • Line 24: The flip() method of the cv2 library flips the frame. It stores the flipped frame in the flipped_frame variable.

  • Line 27: Turns the flipped_frame into a greyscale image using the cvtColor() method.

  • 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_video we defined in line 14.

  • Line 37: Waits for the user to press q if 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.

Copyright ©2024 Educative, Inc. All rights reserved