python - 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 秒。
如果你们能为此提供解决方案,将不胜感激。提前致谢。
解决方案
您应该将作为 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))
推荐阅读
- php - 我试图用codeigniter连接mysql数据库,但它看起来像这样
- mysql - 如何计算列中的值并将每个列名及其结果保存在一行中?
- php - 如何通过url传递变量
- java - 如何将解决方案从递归方法调用传递给调用方法?(回溯算法)
- matlab - 如何绘制这个 Octave\Matlab 函数输出?
- python - 如果在熊猫中已知列(或索引),如何返回值的索引(或列)标签
- android-studio-3.0 - Android Gradle 插件无法识别 SourceSet 'testLocal'。也许你拼错了什么?
- windows-10 - 在 Windows 10 上使用 Python 3.6 时出现 OpenCL 错误
- python - 计算组之间的时间增量
- c++ - 如何为自定义类正确传递指针