[파이썬] opencv-python 비디오 안정화

OpenCV-Python is a popular library for computer vision tasks. In this blog post, we will discuss video stabilization using OpenCV-Python. Video stabilization is a technique used to reduce the effects of camera shake in recorded videos, resulting in smoother and more stable footage.

Background

Camera shake is a common problem when recording videos, especially handheld ones. It can lead to shaky and unstable footage, making it uncomfortable to watch. Video stabilization helps to minimize these unwanted effects by analyzing the motion of the camera and compensating for it in the video sequence.

Approach

The first step in video stabilization is to estimate the motion between consecutive frames. This can be achieved by using feature detection and tracking algorithms like the Lucas-Kanade method or the Shi-Tomasi corner detection algorithm. These algorithms track specific features in the image from frame to frame, allowing us to estimate the motion.

Once the motion is estimated, we can apply a stabilization algorithm to compensate for the detected camera shake. One popular approach is the “optical flow-based method”. This method uses the estimated motion vectors to warp each frame, aligning them with a reference frame or a smoothed version of the video.

Implementation

Let’s dive into the Python code for video stabilization using OpenCV-Python.

import cv2

def stabilize_video(video_path):
    # Open the video file
    video = cv2.VideoCapture(video_path)

    # Get the first frame
    ret, frame = video.read()

    # Convert the frame to grayscale
    prev_frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Create a video writer
    output = cv2.VideoWriter('stabilized_video.mp4', 
                             cv2.VideoWriter_fourcc(*'mp4v'), 
                             video.get(cv2.CAP_PROP_FPS), 
                             (frame.shape[1], frame.shape[0]))

    # Loop through the video frames
    while True:
        # Read the next frame
        ret, frame = video.read()

        # Break the loop if no more frames
        if not ret:
            break

        # Convert the current frame to grayscale
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Calculate the optical flow between frames
        flow = cv2.calcOpticalFlowFarneback(prev_frame_gray, frame_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)

        # Warp the current frame based on the optical flow
        warped_frame = cv2.remap(frame, flow, None, cv2.INTER_LINEAR)

        # Write the stabilized frame to the output video
        output.write(warped_frame)

        # Update the previous frame
        prev_frame_gray = frame_gray

    # Release the video writer and capture object
    output.release()
    video.release()

# Stabilize a video file
stabilize_video('input_video.mp4')

In the above code, we first open the video file using cv2.VideoCapture. We then read the first frame and convert it to grayscale.

Next, we create a cv2.VideoWriter object to write the stabilized frames to a new video file. We specify the output file format, frame rate, and dimensions of the frames.

Inside the main loop, we read the next frame, convert it to grayscale, and calculate the optical flow using cv2.calcOpticalFlowFarneback method. We then warp the current frame based on the calculated flow using cv2.remap.

Finally, we write the stabilized frame to the output video and update the previous frame for the next iteration. Once all frames are processed, we release the video writer and capture object.

Conclusion

Video stabilization is a useful technique to improve the quality of recorded videos. With the help of OpenCV-Python, we can easily implement video stabilization algorithms. This blog post provided an overview of video stabilization and presented an example code for stabilizing videos using OpenCV-Python. Feel free to modify and enhance the code to suit your specific requirements and explore more advanced stabilization algorithms.