首页 > 解决方案 > OpenCV VideoWriter 对象在视频的某些部分速度很快

问题描述

大家好,我是 OpenCV 的新手,我正在尝试为使用网络摄像头录制的视频实现低光视频增强功能。为此,我开发了一个小脚本,它接受一个 inputFile 并识别低对比度帧并为其添加伽马校正。from skimage.exposure import is_low_contrast为此,我将它与 Opencv 一起使用。

这是我用于上述目的的代码。我正在尝试将文件保存为 mp4 格式。

filename = 'video.mp4'

def enhanceVideo(file):
  print('enhancing video')
  cap = cv2.VideoCapture(file)
  out = cv2.VideoWriter(filename, cv2.VideoWriter_fourcc(*'XVID'), 20, (1280, 720))

  while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    text = "Low light: No"
    color = (0, 255, 0)
    if is_low_contrast(gray,  0.35):
        text = "Low light: Yes"
        color = (0, 0, 255)
        # applying gamma correction followed by smoothing to low light frames
        gamma = 2.0
        frame = adjust_gamma(frame, gamma=gamma)
        frame = cv2.medianBlur(frame, 3)


    cv2.putText(frame, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                color, 2)

    out.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

  cap.release()
  out.release()
  cv2.destroyAllWindows()

就功能而言,输出视频是我所期望的,但是一旦将视频写入磁盘,它被识别为低光区域的区域就会比原始 fps 速率更快。因此,长度为 27 秒的输入视频在写入磁盘后的长度为 21 秒。

如果你们能为此提供解决方案,将不胜感激。提前致谢。

标签: pythonopencvimage-processingvideo-processing

解决方案


您应该将作为 FPS20传入VideoWriter的 FPS 替换为您正在打开的视频的实际 FPS。您可以使用以下方法获取 FPS cap.get(cv2.CAP_PROP_FPS)

out = cv2.VideoWriter(filename, cv2.VideoWriter_fourcc(*'XVID'), cap.get(cv2.CAP_PROP_FPS), (1280, 720))

推荐阅读