【发布时间】:2018-11-29 05:51:28
【问题描述】:
我有用于在静止视频上应用背景减除的工作代码,但它不会正确地将减除背景的帧写入其输出文件。我得到了我在cv2.VideoWriter 中指定的 .avi 文件和文件名,但它似乎没有写入我通过的每一帧:
import cv2
import numpy as np
cap = cv2.VideoCapture('traffic-mini.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()
cv2.startWindowThread()
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
out = cv2.VideoWriter('test_output.avi',fourcc, 20.0, (640,480))
while True:
ret, frame = cap.read()
if ret == True:
frame = fgbg.apply(frame)
out.write(frame)
cv2.imshow('fg',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
for i in range (1,5):
cv2.waitKey(1)
输出视频test_output.avi 始终为 6KB,并且没有传入任何帧。我错过了什么?提前致谢
【问题讨论】:
-
尝试将fourcc =
cv2.VideoWriter_fourcc('M','J','P','G')改为fourcc = cv2.VideoWriter_fourcc(*'XVID'),因为可能你的系统没有MJPG的编解码器
标签: python python-3.x opencv computer-vision background-subtraction