【问题标题】:OpenCV VideoWriter object is fast in some parts of the videoOpenCV VideoWriter 对象在视频的某些部分速度很快
【发布时间】:2021-09-05 03:50:09
【问题描述】:

大家好,我是 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 秒。

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

【问题讨论】:

    标签: python opencv image-processing video-processing


    【解决方案1】:

    您应该将作为 FPS 传递给 VideoWriter20 替换为您正在打开的视频的实际 FPS。你可以使用cap.get(cv2.CAP_PROP_FPS)获取FPS:

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

    【讨论】:

    • 谢谢,但问题仍然存在。与原始视频相比,低对比度区域的速度更快。
    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多