我正在尝试将处理后的视频另存为MPEG4文件。原始视频是一个文件。它加载正常,我也可以将其另存为MPEG4。因此,我认为cv2.VideoWriter参数很好。但是,当我尝试进行背景减法,然后进行腐蚀和膨胀时,即使修改后的视频可以正确显示在屏幕上,我也无法保存。代码如下所示:
import cv2
#declare I/O
video_capture = cv2.VideoCapture('C:\\Users\\good.avi')
out = cv2.VideoWriter('output.mp4',cv2.VideoWriter_fourcc(*'MP4V'), 30.0, (800,600))
#background subtraction configruation
subtractor = cv2.createBackgroundSubtractorMOG2(history=10, varThreshold=25)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
if ret != True:
break
#video processing
mask = subtractor.apply(frame)
img_erosion = cv2.erode(mask, None, iterations=3)
img_dilate = cv2.dilate(img_erosion, None, iterations=3)
#show Videos
cv2.imshow("Original", frame)
cv2.imshow("Subtractor", mask)
cv2.imshow("erosion", img_dilate)
#write video DOES NOT WORK. It can saves "frame" but not img_dilate
out.write(img_dilate)
if cv2.waitKey(300) & 0xFF == ord('q'):
break
#When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
out.release