我已经尝试寻找问题的答案,但是我并没有真正找到答案。我想知道如何将视频文件保存在Python 3.5中,该文件将背景扣除应用于它,此外,对输出文件应该保存到何处的位置?
预先感谢
在Anaconda 3.5中尝试使用OpenCV 3.1的代码,可以从视频文件中减去背景,然后将其保存到输出文件
import cv2
import cv2 as cv
import numpy as np
capture = cv2.VideoCapture('people-walking.mp4')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*"DIB ")
video = cv2.VideoWriter('output.avi', fourcc, 30,size)
fgbg= cv2.createBackgroundSubtractorMOG2()
while True:
ret, img = capture.read()
if ret==True:
fgmask = fgbg.apply(img)
video.write(fgmask)
cv2.imshow('forehead',fgmask)
if(cv2.waitKey(27)!=-1):
break
capture.release()
video.release()
cv2.destroyAllWindows()
起初我面临问题。然后将isColor=False
参数添加到VideoWriter
。它解决了问题。
这是代码:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
out = cv2.createBackgroundSubtractorMOG2()
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
output = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480), isColor=False)
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,180)
outmask = out.apply(frame)
output.write(outmask)
cv2.imshow('original', frame)
cv2.imshow('Motion Tracker', outmask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
output.release()
cap.release()
cv2.destroyAllWindows()