2

i have been going through opencv mog and gmg background subtraction, i have installed opencv 3.3.0 from

https://github.com/Itseez/opencv_contrib

and also opencv from the same version, but still i couldn't find MOG not working, where as mog 2 is working,

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

fgbg = cv2.BackgroundSubtractorMOG()

while(1):
   ret, frame = cap.read()

   fgmask = fgbg.apply(frame)

   cv2.imshow('frame',fgmask)
   if cv2.waitKey(1) & 0xFF == ord('q'):
        break 

cap.release()
cv2.destroyAllWindows()

i get the following error message

Traceback (most recent call last):
  File "back.py", line 6, in <module>
    fgbg = cv2.BackgroundSubtractorMOG()
AttributeError: 'module' object has no attribute 'BackgroundSubtractorMOG'
James Z
  • 12,209
  • 10
  • 24
  • 44
Arvind
  • 23
  • 4

2 Answers2

0

The problem is that you are not calling the correct function to create the background substractor...

You can follow the tutorial (it is for version 3.0, but I couldn't find it for 3.3, however it is the same) to have a more in depth explanation of how to use it.

As you can see in the link I provided, you have to call

fgbg = cv2.createBackgroundSubtractorMOG()

instead of

fgbg = cv2.BackgroundSubtractorMOG()

Then the rest is the same.

api55
  • 11,070
  • 4
  • 41
  • 57
  • i created mog and also used fgbg.apply..yet i get there is no attribute as BackgroundSubtractorMog().... – Arvind Jul 12 '17 at 04:53
  • @Arvind it could be that the OpenCV python install is incomplete, I suppose you installed them by sources, are you sure this module was selected? – api55 Jul 12 '17 at 07:44
  • yes,because without contrib being installed background subtraction didnt work,but after i instaleed it works,but only mog didnt work, – Arvind Jul 12 '17 at 08:35
0

fgbg = cv.createBackgroundSubtractorMOG() didn't work for me too but there is another improved version of background subtraction which works similar fgbg = cv2.createBackgroundSubtractorMOG2()

tsukyonomi06
  • 514
  • 1
  • 6
  • 19