python-opencv AttributeError: 'module' 对象没有属性 'createBackgroundSubtractorGMG'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21626619/
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
python-opencv AttributeError: 'module' object has no attribute 'createBackgroundSubtractorGMG'
提问by okkhoy
I am trying to follow the tutorial given in: https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html
我正在尝试按照以下教程进行操作:https: //opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html
While trying the third example (BackgroundSubtractorGMG) I get this error:
在尝试第三个示例 (BackgroundSubtractorGMG) 时,我收到此错误:
AttributeError: 'module' object has no attribute 'createBackgroundSubtractorGMG'
I got the same error for the earlier examples. But I followed the explanation given in this post. some how, the same trick did not work here.
我在前面的例子中遇到了同样的错误。但我遵循了这篇文章中给出的解释。有些方法,同样的技巧在这里不起作用。
If there is some one who has managed to solve it, please help me out.
如果有人已经设法解决它,请帮助我。
Using Python 2.7.3 & opencv 2.4.6.1 on Ubuntu 12.04
在 Ubuntu 12.04 上使用 Python 2.7.3 和 opencv 2.4.6.1
采纳答案by berak
oh dear, that's another one of thosestories ...
哦,亲爱的,这就是的另一个那些故事......
with 2.4.6, you only can use BackgroundSubtractorMOG from python. (full stop)
在 2.4.6 中,您只能使用 Python 中的 BackgroundSubtractorMOG。(句号)
as of 2.4.8, it seems, the BackgroundSubtractorMOG2 problemgot fixed, but the BackgroundSubtractorGMG is still missing.
从 2.4.8 开始,BackgroundSubtractorMOG2 问题似乎得到了解决,但 BackgroundSubtractorGMG 仍然缺失。
with both versions, you use a plain constructor to create one.
对于这两个版本,您可以使用一个普通的构造函数来创建一个。
in 3.0 (master), they changed the syntax, you now have to call 'createBackgroundSubtractorGMG', 'createBackgroundSubtractorMOG2' and such (that's what your tutorial might be refering to). but now you can use all 3 versions at least.
在 3.0 (master) 中,他们更改了语法,您现在必须调用“createBackgroundSubtractorGMG”、“createBackgroundSubtractorMOG2”等(这可能是您的教程所指的内容)。但现在您至少可以使用所有 3 个版本。
so in any way, if you want to use BackgroundSubtractorMOG2 , you'll have to update to 2.4.8, if you need BackgroundSubtractorGMG, you'll need 3.0 (which is 'bleeding edge' in a way, but the new interface has the far better control over the params needed, imho).
所以无论如何,如果你想使用 BackgroundSubtractorMOG2 ,你必须更新到 2.4.8,如果你需要 BackgroundSubtractorGMG,你需要 3.0(在某种程度上这是“出血边缘”,但新界面具有更好地控制所需的参数,恕我直言)。
回答by Sven Zwei
In OpenCV 3.0.0-dev, you have to compile with the contrib repos and then it's in the bgsegm sub-module. I.e. just call cv2.bgsegm.createBackgroundSubtractorGMG()
在 OpenCV 3.0.0-dev 中,您必须使用 contrib 存储库进行编译,然后它位于 bgsegm 子模块中。即只需调用 cv2.bgsegm.createBackgroundSubtractorGMG()
回答by Sikander SD
cv2.bgsegm.createBackgroundSubtractorGMG()
cv2.createBackgroundSubtractorMOG2()
cv2.bgsegm.createBackgroundSubtractorMOG(),
**this worked for me **
**这对我有用**
回答by Uday Posia
bgsegm was in contrib module of opencv, but after the update I am not sure.
bgsegm 在 opencv 的 contrib 模块中,但更新后我不确定。
But still, if you have not built contrib module:
但是,如果您还没有构建 contrib 模块:
pip install opencv-contrib-python
Make sure no console is running that has imported cv2 while you execute your installing process.
Run the cmdas Administration
确保在执行安装过程时没有运行已导入 cv2 的控制台。cmd以管理身份运行
It worked for me.
它对我有用。

