OpenCV VideoWriter 에서 매개변수 FPS

OpenCV VideoWriter 에서 매개변수 FPS(frame per second)

매개변수 FPS 는 고정된 값이다.
카메라에서 영상을 읽을 때, FPS 는 영상마다 조금씩 다르다.

이를 보정하는 방법을 Python 으로 보여주는 예제가 있어 소개한다.

질문) Variable fps (frame per second) in cv2
응답) User Markus - Stack Overflow
링크) python - Variable fps (frame per second) in cv2 - Stack Overflow

import math
import numpy as np
import cv2

image_size = 200
src_fps = 25
dst_fps = 25


# create sample frame with four rotating balls
def create_sample_frame(size, frame_no, fps):
    img = np.zeros((size, size, 3), dtype=np.uint8)
    ctr = size // 2

    for i in range(1, 5):
        phi = -frame_no / fps * i
        r = size * (0.5 - 0.1 * i)
        cv2.circle(img, (round(math.sin(phi) * r + ctr), round(math.cos(phi) * r + ctr)), size // 30, (0, 255, 0), -1)

    return img


frames = [create_sample_frame(image_size, i, src_fps) for i in range(200)]
height, width, layers = frames[0].shape
video = cv2.VideoWriter('video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), dst_fps, (width, height))

# ramp array with number of destination frames, start fps of source, end fps of source
fps_ramp = [[100, 5, 5], [100, 5, 25], [120, 25, 25]]

src_pos = 0
dst_pos = 0

for n, start_src_fps, end_src_fps in fps_ramp:
    for i in range(n):
        print(f"writing source frame {int(src_pos)} to destination frame {dst_pos}")
        video.write(frames[round(src_pos)])
        dst_pos += 1
        cur_fps = (end_src_fps - start_src_fps) * (i / n) + start_src_fps
        src_pos += cur_fps / src_fps

video.release()
1개의 좋아요