Ask Your Question

How to give createBackgroundSubtractorMOG2 selective history images.

asked 2017-11-17 12:44:03 -0500

maxisme gravatar image

updated 2017-11-17 17:37:25 -0500

I am currently running this to acquire images:

 fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows=False)
 for frame in, format="rgb", use_video_port=True):
    img = frame.array
    fgmask = fgbg.apply(img)

How can I go about choosing which images to save in the history. I want to build a function where I can select which images are background. For example initially the first 50 input images will be used as history for the subtractor and after that all masks that have under a certain threshold of difference will be used as history too. I basically do not want the new foreground images to be used as history is that possible?

I have written this code:

fgmask = fgbg.apply(img, bg_learning_rate)

# check if should learn from content
perc_black = np.count_nonzero(fgmask) / len(fgmask)
if perc_black > MOVEMENT_THRESH:
    # something is happening in image
    bg_learning_rate = 0
    bg_learning_rate = -1

but this teaches the system to not use the next image in the model which is not that useful although highly probable that it will still feature a large object.

I am doing this to extract people from a static background.

edit retag flag offensive close merge delete


no, not possible,bad idea in the 1st place.

(you, as a human, might be able to tell "good" from "bad" images. -- your machine can't)

berak gravatar imageberak ( 2017-11-17 13:31:38 -0500 )edit

Can you expand a bit? If I leave it to train on 500 images first of all. After that I set a blackness threshold and if it exceeds it there must be a new dominating object in which is not a background therefore why would I train it to think that it is a background?

maxisme gravatar imagemaxisme ( 2017-11-17 17:34:28 -0500 )edit

What if the object was in the image in the first 500 frames? Then you would be rejecting the background, and keeping the object.

Tetragramm gravatar imageTetragramm ( 2017-11-17 18:59:52 -0500 )edit

I can tell you for a fact it won't be.

maxisme gravatar imagemaxisme ( 2017-11-18 03:49:17 -0500 )edit

Then you should turn the learning rate to 0, apply, check, then if it should be included, turn the learning rate to -1 and apply again. Messy, but it should do what you want.

Tetragramm gravatar imageTetragramm ( 2017-11-18 08:44:40 -0500 )edit

See my code above!

maxisme gravatar imagemaxisme ( 2017-11-18 09:27:53 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-18 10:06:38 -0500

Tetragramm gravatar image

My suggestion is different than your code above. You should always apply with learning rate = 0, then if it is a good image, in your terms, then apply again with an actual learning rate.

fgmask = fgbg.apply(img, 0)

# check if should learn from content
perc_black = np.count_nonzero(fgmask) / len(fgmask)
if perc_black <= MOVEMENT_THRESH:
    fgmask = fgbg.apply(img, -1)
edit flag offensive delete link more


I am getting a lot of noise which seems to be just background content for example this is the actual background and this is the masked image - notice the clock at the top and the painting on the left, for some reason they are being presented in the mask any idea why?

maxisme gravatar imagemaxisme ( 2017-11-18 15:06:25 -0500 )edit

Applied erosion and dilation to it and getting really good results!!

maxisme gravatar imagemaxisme ( 2017-11-20 12:14:26 -0500 )edit

Question Tools

1 follower


Asked: 2017-11-17 12:44:03 -0500

Seen: 1,194 times

Last updated: Nov 18 '17