Python3|Python3 AttributeError: module 'cv2' has no attribute 'createBackgroundSubtractorMOG'
问题:在用python3使用cv2.createBackgroundSubtractorMOG()的时候,可能会产生错误:AttributeError: module 'cv2' has no attribute 'createBackgroundSubtractorMOG'
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG()
while(1):
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
cv2.imshow('frame',fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
报错信息:
---------------------------------------------------------------------------
AttributeErrorTraceback (most recent call last)
in ()
2 import cv2
3 cap = cv2.VideoCapture(0)
----> 4 fgbg = cv2.createBackgroundSubtractorMOG()
5 while(1):
6ret, frame = cap.read()AttributeError: module 'cv2' has no attribute 'createBackgroundSubtractorMOG'
解决:将fgbg = cv2.createBackgroundSubtractorMOG()替换为:
fgbg = cv2.createBackgroundSubtractorMOG2()
分析:
在python3中,缺少cv2.createBackgroundSubtractorMOG()方法改为了cv2.createBackgroundSubtractorMOG2()。
变更样例:
fgbg = cv2.createBackgroundSubtractorMOG2()
说明:问题产生的环境
Python版本:3.6.5
OpenCV版本:3.4.2
【Python3|Python3 AttributeError: module 'cv2' has no attribute 'createBackgroundSubtractorMOG'】转自:https://blog.csdn.net/yuxuan_08/article/details/87966904
推荐阅读
- 没有导入future这个module
- Python3|Python3 MySQL 数据库连接
- win10环境|win10环境 python3.6安装pycrypto-2.6.1的问题
- 【React|【React Native填坑之旅】从源码角度看JavaModule注册及重载陷阱
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- 运行报错Cannot|运行报错Cannot find module '@babel/compat-data/corejs3-shipped-proposals’
- python3|python3 模块 包
- Python|Python TypeError: 'module' object is not callable 原因分析
- react|react Cannot find module 'node_modules/_react-scripts/config/webpack.config.dev
- Python3.x(Socket网络编程)