OpenCV VideoWriter对象在视频的某些部分是快速的



大家好,我是OpenCV的新手,我正在尝试为使用网络摄像头录制的视频实现微光视频增强功能。为此,我开发了一个小脚本,它使用inputFile来识别低对比度帧,并添加gamma校正。为此,我使用了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秒。

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

您应该将传递到VideoWriter中的20替换为您正在打开的视频的实际FPS。您可以使用cap.get(cv2.CAP_PROP_FPS):获得FPS

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

最新更新