Ask Your Question

Replace background in video by opencv

asked 2015-03-26 00:15:00 -0500

john_1282 gravatar image

updated 2015-08-25 18:35:27 -0500

I am looking for method and implementation to replace background in video. My goal looks like Youtube video. The author said that he used threshold value to extract background and replace them. However, I don't believed that threshold can acheived very nice result. Could you suggest to me any method that available in opencv for real time replacing background in opencv. Thank you in advance

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-26 04:40:04 -0500

kbarni gravatar image

I think he uses thresholding, but not on the image itself, but on the difference of two images.

Probably this application uses a fixed camera and he grabbed a reference frame that contains only the background. The application would work like this (pseudocode):

capture(referenceframe)  //get a frame which doesn't contain any foreground
    capture(newframe)  //capture a new frame
    read(backgroundframe)  //read (generate or capture) frame that will be the replaced background
    diff=abs(newframe-referenceframe) //calculate the difference between the two frames
    thresholdedDiff=threshold(diff,thresholdvalue);  //threshold
    denoisedDiff=open(thresholdDiff)  //some denoising
   // or: foreach pixel in denoisedDiff : if pixel==1 : resultpixel=newframepixel, else : resultpixel=backgroundpixel

I hope you get the idea, it isn't difficult at all. However it has some limitations, it won't work if the camera moves or the illumination changes (you have to update the reference frame).

edit flag offensive delete link more


If your lightning changes gradually than you need to apply background foregorund segmentation based on gaussian mixture models, they will model the background variance and they work pretty well for outdoor conditions.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-26 05:54:36 -0500 )edit

Thank Kbami and StevenPuttemans. For background changing, we can using MOG and GOG model, right? I found some tutorial of that problem at, Right?

john_1282 gravatar imagejohn_1282 ( 2015-03-26 09:43:42 -0500 )edit

Yes, you can use the BackgroundSubstractorMOG and BackgroundSubstractroMOG2 classes too: . I don't know if the BackgroundSubtractorGMG class is till present in OpenCV.

kbarni gravatar imagekbarni ( 2015-03-26 10:00:23 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2015-03-26 00:15:00 -0500

Seen: 1,287 times

Last updated: Mar 26 '15