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
Just for the record. As suggested by dan-mašek , the fgmask
is a 1-channel image, thus the VideoWriter
fails showing the following error: (-215) scn==2 & (scn == 3 || scn == 4)
. I solved passing the isColor=False
argument to VideoWriter
.
I was facing similar problem. Add isColor=False
argument to the VideoWriter
.
Change certain portion of your code accordingly as shown:
video = cv2.VideoWriter('harsha_subtractor.avi', fourcc, 10.0, size,
isColor=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.