singhuist
singhuist

Reputation: 302

How to separate background from image?

I am using OpenCV 3.1 with Python 2.7. I am trying to separate background and foreground objects and have used this:

img = cv2.imread('testimage.jpg', cv2.IMREAD_COLOR);
fgbg = cv2.BackgroundSubtractorMOG2();
fgmask = fgbg.apply(img);
cv2.imshow("backsep",fgmask);

When I run this, I get the following error:

Traceback (most recent call last): File "C:/Users/R.K.singh/Desktop/Image processing/background-sep.py", line 20, in fgbg = cv2.BackgroundSubtractorMOG2(); AttributeError: 'module' object has no attribute 'BackgroundSubtractorMOG2'

Please help.

P.S: This question has been asked before however none of the solutions work.

Upvotes: 1

Views: 7732

Answers (1)

Jeru Luke
Jeru Luke

Reputation: 21243

As Axel suggested cv2.BackgroundSubtractorMOG2() is used to remove the background from video frames.

When it comes to images you can use the GrabCut Algorithm . The OpenCV documentation contains a tutorial including the relevant Python code.

You can also watch these videos for better understanding of Image Segmentation:

Upvotes: 6

Related Questions