I have a input text file which contains the names of the images which I have to put together into a video and then apply background subtraction on the video and then save the output.
I tried this:
# convert images to video
f = open('files_harsha.txt','r+')
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('harsha_output.avi',fourcc, 10.0, (1344,1024))
for line in f:
frame=cv2.imread(line.strip())
out.write(frame)
# cv2.imshow('1',frame)
# cv2.waitKey(0)
out.release()
The above snippet works correctly, saving the video. Then I perform the background subtraction and try to save the output, as shown below:
capture = cv2.VideoCapture('harsha_output.avi')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
video = cv2.VideoWriter('harsha_subtractor.avi', fourcc, 10.0,size)
fgbg= cv2.createBackgroundSubtractorMOG2(varThreshold=50,history=4)
while (1):
ret, img = capture.read()
if ret==True:
fgmask = fgbg.apply(img)
video.write(fgmask)
#cv2.imshow('forehead',fgmask)
else:
capture.release()
video.release()
break
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cv2.destroyAllWindows()
I am able to see the background subtracted video but I am unable to save the file. Why am I having this issue?
I am using Python 2.7 within the Anaconda IDE (Spyder) and am using openCV 3.x