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

    推荐阅读