3

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!

user3483746
  • 155
  • 3
  • 4
  • 13
  • @Numbers I have added what I have tried. I don'tt know whether I'm right or not. – user3483746 Apr 27 '14 at 16:41
  • [duplicate](http://stackoverflow.com/questions/18721552/opencv2-python-createbackgroundsubtractor-module-not-found) – Hadi Apr 27 '14 at 16:58
  • @Constantine Its for the video I guess, but not for the still image. I want the background subtraction for still image. – user3483746 Apr 27 '14 at 17:18
  • You could hack and "train" the MOG2 with your stil image and insert the other one as a foreground. – Sebastian Schmitz Apr 28 '14 at 08:01
  • @user3483746 If you only give the algorithm a single image, it cannot know what is foreground and what is background unless you somehow tell him ... – BConic Apr 28 '14 at 08:07
  • Have you tried Core.absdiff(bkgrndImage, image, destImg)? – medloh Apr 28 '14 at 21:03
  • What version of OpenCV are you using? I think this function is only available in 3.0.0-dev – 01zhou Apr 29 '14 at 21:38
  • @medloh is this function available in opencv 2.4.6?? `Core.absdiff()`? – user3483746 May 03 '14 at 17:24
  • I'm only familiar with the Java bindings. I know it was there in 2.4.7, and 99% sure it was in 2.4.6. Not sure what the comparable call in Python would be. – medloh May 05 '14 at 15:50
  • 1
    Replace `createBackgroundSubtractorMOG2` with `BackgroundSubtractorMOG` – Tzach Aug 28 '14 at 18:02
  • Possible duplicate of [OpenCV2 Python createBackgroundSubtractor module not found](https://stackoverflow.com/questions/18721552/opencv2-python-createbackgroundsubtractor-module-not-found) – Moia Sep 13 '19 at 08:20

1 Answers1

0
fgbg = cv2.createBackgroundSubtractorMOG2()

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

fgbg = cv2.BackgroundSubtractorMOG2()
jtlz2
  • 7,700
  • 9
  • 64
  • 114