How to subtract the background from an image - python , opencv

user3483746 picture user3483746 · Apr 27, 2014 · Viewed 9.9k times · Source

How to subtract the background in a still image? IF the issue is with videos then we can use cv2.createBackgroundSubtractorMOG(). I tried to manipulate the function, but I got AttributeError

This is what i have tried:

img = cv2.imread('lena.png')
fgbg = cv2.createBackgroundSubtractorMOG2()
fgmask = fgbg.apply(img)
cv2.imwrite('{0:d}.jpg'.format(i),fgmask)

But I got this error:

AttributeError: 'module' object has no attribute 'createBackgroundSubtractorMOG2'

Any suggestion are welcome!

Answer

Siraj Muhammad picture Siraj Muhammad · Aug 24, 2016
fgbg = cv2.createBackgroundSubtractorMOG2()

is not available in opencv3 tutorial - instead you can use:

fgbg = cv2.BackgroundSubtractorMOG2()