Efficient Background subtraction with OpenCV

亡梦爱人 提交于 2019-11-27 00:15:28

问题


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 foreground 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.


回答1:


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);



回答2:


http://vimeo.com/27477093

code is here

http://code.google.com/p/derin-deli-mavi/downloads/detail?name=denemeOpenCv23.zip&can=2&q=

to reach a colored foreground just copy image by using foreground mask

// image.copyTo(foreground,foreground);



来源:https://stackoverflow.com/questions/2026752/efficient-background-subtraction-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!