views:

1825

answers:

2

Hi,

I want to do background subtraction in a video file using Opencv method. Right now i'm able to do background subtraction, but the problem is that i couldn't get the output in color mode. All the output after subtracting the background is coming in grayscale color mode :(. I want to get the color information to the foreground which is the resulting output after background got subtracted.

Can i do it using masking technique?? like the following procedure which i'm thinking about.

  1. Capture Input -- InputFrame (RGB)
  2. Process InputFrame
  3. Subtract background, store forground in TempFrame ( which is coming in grayscale :( )
  4. Create a mask using TempFrame
  5. Apply the created mask to the InputFrame
  6. Get colored foreground as OutFrame

I'm struck up with doing the masking using Opencv. I'm just a very beginner in OpenCV. Please help me to overcome this.

Thanks in advance.

+3  A: 

Okay, I don't understand how TempFrame (your foreground) could be greyscale if you are using background subtraction. You must be using a very special algorithm. But assuming TempFrame is greyscale, then you would do this:

cv::Mat mask = tempFrame > 0.5;

cv::Mat outFrame;
capturedFrame.copyTo(outFrame, mask);

That is OpenCV 2.0 code above. The number 0.5 is a threshold, you'll need to set it to something appropriate. If you're not using floating-point images, you'd probably set it to 128 or something like that. This is the same thing in OpenCV 1.1 code:

CvMat* mask = cvCreateMat(tempFrame.rows, tempFrame.cols, CV_8UC1);
cvCmpS(tempFrame, 0.5, mask);

CvMat* outFrame = cvCreateMat(capturedFrame.rows, capturedFrames.cols, CV_32FC3);
cvCopy(capturedFrame, outFrame, mask);
Ray Hidayat
Thank you, But i still wonder why all the examples and code for BGS i come across the internet generate only Grayscale output? Anyways.. Thanks again.
Jabez
Oh okay, I suppose it's just easier to do background subtraction with greyscale so maybe that's why.
Ray Hidayat
A: 

Despite colour providing vastly richer information than grayscale, it is harder to work with, as there is no clear and unambiguously good way to handle the colour channels. This makes academics unhappy, as they can't come up with nice proofs very easily, hence they pretend that we are still in the much simpler grayscale world of 1950. ho hum.

Stu