我试图在python中保存背景扣除视频,以下是我的代码.
import cv2 import numpy as np capture = cv2.VideoCapture('MAH00119.mp4') size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'X264') out = cv2.VideoWriter('output.mp4', -1 , 20.0 , size) fgbg= cv2.createBackgroundSubtractorMOG2() while True: ret, img = capture.read() if ret==True: fgmask = fgbg.apply(img) out.write(fgmask) cv2.imshow('img',fgmask) if(cv2.waitKey(27)!=-1): break capture.release() out.release() cv2.destroyAllWindows()
但是,这会引发以下错误:"找不到OpenCV:FFMPEG:标签0xffffffff /' '(格式为'mp4/MP4(MPEG-4 Part 14)')'"
我安装了FFMPEG并将其添加到环境变量中.我的背景减法代码无需保存到文件工作正常,所以我知道openCV安装没有任何问题.我被困在这个地方.我知道我的python似乎没有识别FFMPEG,但我不知道除了将FFMPEG添加到环境变量之外还有什么可做的.我在Windows 10和Python 2.7上使用OpenCV 3.2版.
任何帮助都感激不尽!
修改了一点代码.它适用于我的PC,在Windows 10 64位上使用OpenCV 3.2 for Python 2.7.
import cv2 import numpy as np capture = cv2.VideoCapture('./videos/001.mp4') size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'DIVX') # 'x264' doesn't work out = cv2.VideoWriter('./videos/001_output.mp4',fourcc, 29.0, size, False) # 'False' for 1-ch instead of 3-ch for color fgbg= cv2.createBackgroundSubtractorMOG2() while (capture.isOpened()): #while Ture: ret, img = capture.read() if ret==True: fgmask = fgbg.apply(img) out.write(fgmask) cv2.imshow('img',fgmask) #if(cv2.waitKey(27)!=-1): # observed it will close the imshow window immediately # break # so change to below if cv2.waitKey(1) & 0xFF == ord('q'): break capture.release() out.release() cv2.destroyAllWindows()
检查这对参数设置上cv2.VideoWriter() .
希望这有帮助.