Home > Enterprise >  OpenCV BackgroundSubtractor Thinks A Busy Foreground Is The Background
OpenCV BackgroundSubtractor Thinks A Busy Foreground Is The Background

Time:02-01

I am using background subtraction to identify and control items on a conveyor belt. The work is very similar to the typical tracking cars on a highway example. I start out with an empty conveyor belt so the computer knows the background in the beginning. But after the conveyor has been full of packages for a while the computer starts to think that the packages are the background. This requires me to restart the program from time to time. Does anyone have a workaround for this? Since the conveyor belt is black I am wondering if it maybe makes sense to toss in a few blank frames from time to time or if perhaps there is some other solution. Much Thanks


# After experimentation, these values seem to work best.
backSub = cv2.createBackgroundSubtractorMOG2(history = 5000, varThreshold = 1000, detectShadows = False)    

while True:
    return_value, frame = vid.read()
    
    if frame is None:
        print('Video has ended or failed, try a different video format!')
        break        
    
        
    ## [gaussian blur helps to remove noise These settings seem to remove reflections from rollers.] 
    blur = cv2.GaussianBlur(frame, (0,0), 5, 0, 0)
    ## [End of: gaussian blur helps to remove noise These settings seem to remove reflections from rollers.]
    

    ## [Remove the background and produce a grays scale image]
    fgMask = backSub.apply(blur)       
    ## [End of: Remove the background and produce a grey scale image]

CodePudding user response:

You say you're giving the background subtractor some pure background initially.

When you're done with that and in the "running" phase, call apply() specifically with learningRate = 0. That ensures the model won't be updated.

  •  Tags:  
  • Related