根据官方文档,我想知道python中opencv的backroundSubractor类的行为, 所有孩子都应该有apply和getBackgroundImage。但是,如果我调整 python 示例以显示背景而不是前景,它适用于:
- createBackgroundSubtractorGSOC
- createBackgroundSubtractorCNT
- createBackgroundSubtractorKNN
- createBackgroundSubtractorLSBP
- 创建背景减法器MOG2。
但是对于 2 它不起作用:
- createBackgroundSubtractorMOG
- createBackgroundSubtractorGMG
这种行为是有意的还是出了什么问题?
我的代码:
import numpy as np
import cv2
cap = cv2.VideoCapture('vtest.avi')
fgbg=cv2.bgsegm.createBackgroundSubtractorMOG()
#works
#fgbg=cv2.bgsegm.createBackgroundSubtractorGSOC()
#fgbg=cv2.bgsegm.createBackgroundSubtractorCNT()
#fgbg=cv2.createBackgroundSubtractorKNN()
#fgbg=cv2.bgsegm.createBackgroundSubtractorLSBP()
#fgbg = cv2.createBackgroundSubtractorMOG2(100, 16, 0)
#error
#fgbg=cv2.bgsegm.createBackgroundSubtractorMOG()
#fgbg=cv2.bgsegm.createBackgroundSubtractorGMG()
while(1):
ret, frame = cap.read()
height = np.size(frame,0)
width = np.size(frame,1)
blank_image = np.zeros((height, width, 3), np.uint8)
fgmask = fgbg.apply(frame)
bgmask=fgbg.getBackgroundImage()
cv2.imshow('frame', bgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()