OpenCV2 Python createBackgroundSubtractor module n

2019-02-05 23:12发布

I am trying to use cv2.createBackgroundSubtractorMOG2 () method in Python. I have tried both on my Mac and on my Raspberry Pi, and get the same error when running the following line of code:

fgbg = cv2.createBackgroundSubtractorMOG2()

The code I am using is taken from https://github.com/abidrahmank/OpenCV2-Python-Tutorials/blob/master/source/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.rst

I get the following error when running this code:

fgbg = cv2.createBackgroundSubtractorMOG2() AttributeError: 'module' object has no attribute 'createBackgroundSubtractorMOG2'

I can't seem to use any of the createBackgroundSubtractor methods.

I have been trying to solve this for the past day, but I have had no luck searching online, as there is limited support for cv2 on Python.

Thanks in advance

7条回答
smile是对你的礼貌
2楼-- · 2019-02-05 23:32

Thanks for the comments all. It seems that createBackgroundSubtractorMOG2() hasn't been added to OpenCV 2.4, but it is present in master branch, which can be compiled from github.

I am finding that cv2.BackgroundSubtractor() is working for what I need at the moment.

See http://code.opencv.org/issues/2812#note-5 for more details.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-05 23:35

Replace the create.... with

fgbg = cv2.BackgroundSubtractorMOG()

查看更多
闹够了就滚
4楼-- · 2019-02-05 23:38

I'm using OpenCV-python 2.4.9, and Python2.7.8.

In my environment, cv2.BackgroundSubtracorMOG and cv2.BackgroundSubtractorMOG2 are available.

You can find out what attributes are available by using "help(cv2)" in your python shell.

BackgroundSubtractorMOG(...)
    BackgroundSubtractorMOG([history, nmixtures, backgroundRatio[, noiseSigma]]) -> <BackgroundSubtractorMOG object>

BackgroundSubtractorMOG2(...)
    BackgroundSubtractorMOG2([history, varThreshold[, bShadowDetection]]) -> <BackgroundSubtractorMOG2 object>
查看更多
Animai°情兽
5楼-- · 2019-02-05 23:41
>>> import cv2
>>> cv2.__version__
>>> 3.2.0
>>>bg_model = cv2.BackgroundSubtractorMOG2(0, 10)
Traceback (most recent call last):
  File "/home/manivannan/pythonexamle/opencv/Samples/hand-gesture-recognition-opencv/HandRecognition.py", line 233, in <module>
    bg_model = cv2.BackgroundSubtractorMOG2(0, 10)
AttributeError: 'module' object has no attribute 'BackgroundSubtractorMOG2'
>>>bg_model = cv2.createBackgroundSubtractorMOG2(0, 10)

Use createBackgroundSubtractorMOG2 instead of BackgroundSubtractorMOG2 It's Working

查看更多
\"骚年 ilove
6楼-- · 2019-02-05 23:44

I'm using

>>> import cv2
>>> cv2.__version__
>>> 3.2.0

and python 2.7.12. While I tried to use cv2.createBackgroundSubtractorMOG() I received the same error message (also tried without "create..."). But I was surprised when I discovered cv2.createBackgroundSubtractorKNN() with the same functionality instead ... and the test code works :) 2 days I was confused and couldn't find where the problem is.

查看更多
▲ chillily
7楼-- · 2019-02-05 23:46

According to https://docs.opencv.org/master/db/d5c/tutorial_py_bg_subtraction.html You should use cv.bgsegm.createBackgroundSubtractorMOG()

I tested it on 3.4.0-dev and it worked.

Example Code:

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('vtest.avi')
fgbg = cv.bgsegm.createBackgroundSubtractorMOG()
while(1):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    cv.imshow('frame',fgmask)
    k = cv.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv.destroyAllWindows()
查看更多
登录 后发表回答