找不到 OpenCV2 Python createBackgroundSubtractor 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18721552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV2 Python createBackgroundSubtractor module not found
提问by user2765323
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:
我正在尝试在 Python 中使用 cv2.createBackgroundSubtractorMOG2 () 方法。我在 Mac 和 Raspberry Pi 上都尝试过,运行以下代码行时出现相同的错误:
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'
fgbg = cv2.createBackgroundSubtractorMOG2() AttributeError: 'module' 对象没有属性 'createBackgroundSubtractorMOG2'
I can't seem to use any of the createBackgroundSubtractor methods.
我似乎无法使用任何 createBackgroundSubtractor 方法。
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.
过去一天我一直在尝试解决这个问题,但我没有在线搜索运气,因为 Python 上对 cv2 的支持有限。
Thanks in advance
提前致谢
回答by user2765323
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.
感谢大家的评论。似乎 createBackgroundSubtractorMOG2() 尚未添加到 OpenCV 2.4,但它存在于 master 分支中,可以从 github 编译。
I am finding that cv2.BackgroundSubtractor() is working for what I need at the moment.
我发现 cv2.BackgroundSubtractor() 正在为我目前需要的东西工作。
See http://code.opencv.org/issues/2812#note-5for more details.
有关更多详细信息,请参阅http://code.opencv.org/issues/2812#note-5。
回答by user2897775
Replace the create.... with
将 create.... 替换为
fgbg = cv2.BackgroundSubtractorMOG()
fgbg = cv2.BackgroundSubtractorMOG()
回答by fury
cv2.createbackgroundSubstractor()
works in cv 3.0 for 2.4.x use cv2.BackgroundSubstractor()
cv2.createbackgroundSubstractor()
适用于 cv 3.0 以供 2.4.x 使用 cv2.BackgroundSubstractor()
回答by wata
I'm using OpenCV-python 2.4.9, and Python2.7.8.
我使用的是 OpenCV-python 2.4.9 和 Python2.7.8。
In my environment, cv2.BackgroundSubtracorMOG and cv2.BackgroundSubtractorMOG2 are available.
在我的环境中, cv2.BackgroundSubtracorMOG 和 cv2.BackgroundSubtractorMOG2 可用。
You can find out what attributes are available by using "help(cv2)" in your python shell.
您可以通过在您的 python shell 中使用“help(cv2)”来找出可用的属性。
BackgroundSubtractorMOG(...)
BackgroundSubtractorMOG([history, nmixtures, backgroundRatio[, noiseSigma]]) -> <BackgroundSubtractorMOG object>
BackgroundSubtractorMOG2(...)
BackgroundSubtractorMOG2([history, varThreshold[, bShadowDetection]]) -> <BackgroundSubtractorMOG2 object>
回答by Ievgen S.
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.
和蟒蛇 2.7.12。当我尝试使用 cv2.createBackgroundSubtractorMOG() 时,我收到了相同的错误消息(也尝试不使用“create...”)。但是当我发现 cv2.createBackgroundSubtractorKNN() 具有相同的功能时,我感到很惊讶......并且测试代码有效:) 2 天我很困惑,找不到问题所在。
回答by Manivannan Murugavel
>>> 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
使用 createBackgroundSubtractorMOG2 而不是 BackgroundSubtractorMOG2 它正在工作
回答by Qin Heyang
According to https://docs.opencv.org/master/db/d5c/tutorial_py_bg_subtraction.htmlYou should use cv.bgsegm.createBackgroundSubtractorMOG()
根据https://docs.opencv.org/master/db/d5c/tutorial_py_bg_subtraction.html你应该使用cv.bgsegm.createBackgroundSubtractorMOG()
I tested it on 3.4.0-dev and it worked.
我在 3.4.0-dev 上对其进行了测试,并且有效。
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()