我正在尝试从视频中提取背景图像,以便检测其中的移动物体。我找到了 cv2.BackgroundSubtractorMOG() 之类的函数,但是我无法让它工作。有人有使用这个的经验吗?
我创建了对象mog = cv2.BackgroundSubtractorMOG(300,-1,-1,-1)
然后我尝试mog.apply(Nmat,Nforemat,-1)
,但这似乎不起作用,我收到以下错误:
......\OpenCV-2.4.0\modules\video\src\bgfg_gaussmix.cpp:117: 错误: (-215) CV_MAT_DEPTH(frameType) == CV_8U
Nmat 和 N foremat 是 numpy 数组,因为如果不是,我也会收到错误消息。
这是正在进行的工作...
import cv
import cv2
import numpy as np
if __name__ == '__main__':
cv.NamedWindow("test1", cv.CV_WINDOW_AUTOSIZE)
cv.NamedWindow("test2", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CreateFileCapture('test.avi')
frame = cv.QueryFrame(capture)
img = cv.CreateImage(cv.GetSize(frame),8,1)
thresh = cv.CreateImage(cv.GetSize(frame),8,1)
foreground = cv.CreateImage(cv.GetSize(frame),8,1)
foremat = cv.GetMat(foreground)
Nforemat = np.array(foremat, dtype=np.float32)
thresh = cv.CreateImage(cv.GetSize(img),8,1)
mog = cv2.BackgroundSubtractorMOG()
loop = True
nframes=0
while(loop):
frame = cv.QueryFrame(capture)
mat = cv.GetMat(frame)
Nmat = np.array(mat, dtype=np.float32)
cv.CvtColor(frame,img,cv.CV_BGR2GRAY)
if (frame == None):
break
mog.apply(Nmat,Nforemat,-1)
cv.Threshold(img,thresh,100,255,cv.CV_THRESH_BINARY)
cv.ShowImage("test1", thresh)
cv.ShowImage("test2",frame)
char = cv.WaitKey(50)
if (char != -1):
if (char == 27):
break
cv.DestroyWindow("test1")
cv.DestroyWindow("test2")