如何在OpenCV2 Python中保存视频蒙版 [英] How to save masks of videos in openCV2 python
问题描述
我可以从网络摄像头捕获视频,并使用此代码很好地保存
I can capture a video from the webcam and save it fine with this code
cap = cv2.VideoCapture(0)
fgbg= cv2.BackgroundSubtractorMOG()
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret,frame = cap.read()
if ret:
fgmask = fgbg.apply(frame)
out.write(frame) #Save it
cv2.imshow('Background Subtraction', fgmask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break #q to quit
else:
break #EOF
cap.release()
out.release()
cv2.destroyAllWindows()
这将它记录下来,并显示背景减法.它将其保存到output.avi.一切都很好.但是我无法保存前景遮罩,它给了我一个Could not demultiplex stream
错误. (在上面的代码中此行已更改.)
This records it as one would expect, and shows the background subtraction thing also. It saves it to output.avi. All is well. But I can't save the foreground mask, it gives me a Could not demultiplex stream
error. (This line is changed in the code above).
out.write(fgmask) #Save it
这是为什么? fgmask是否不是像我从捕获中读取时那样的框架?
Why is this? Is the fgmask not a frame like when I am reading from the capture?
推荐答案
好了!让我知道是否有更有效的方法可以做到这一点,或者我是否缺少某些东西.
Alright figured it out! Let me know if there's a more efficient way to do this or if I am missing something..
在背景减法中生成的前景蒙版是一个8位二进制图像,因此我们必须将其转换为其他格式.可能存在更好的一个,但是我使用了RGB
The foreground mask generated in background subtraction is an 8bit binary image, so we have to convert it to a different format. Probably a better one exists, but I used RGB
frame = cv2.cvtColor(fgmask, cv2.COLOR_GRAY2RGB)
这篇关于如何在OpenCV2 Python中保存视频蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!