Ask Your Question
1

Replace background in video by opencv

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

john_1282 gravatar image

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

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
5

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

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
do
    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
    result=denoisedDiff*newframe+(1-denoisedDiff)*backgroundframe; 
   // or: foreach pixel in denoisedDiff : if pixel==1 : resultpixel=newframepixel, else : resultpixel=backgroundpixel
    imshow(newframe,backgroundframe,result);
while(!keypressed)

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

Comments

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 -0600 )edit

Thank Kbami and StevenPuttemans. For background changing, we can using MOG and GOG model, right? I found some tutorial of that problem at http://stackoverflow.com/questions/15..., Right?

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

Yes, you can use the BackgroundSubstractorMOG and BackgroundSubstractroMOG2 classes too: http://docs.opencv.org/modules/video/... . I don't know if the BackgroundSubtractorGMG class is till present in OpenCV.

kbarni gravatar imagekbarni ( 2015-03-26 10:00:23 -0600 )edit

Question Tools

2 followers

Stats

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

Seen: 2,912 times

Last updated: Mar 26 '15