Ask Your Question
1

Background substraction using OpenCV MOG from live camera feed.

asked 2014-08-05 16:47:33 -0500

Linus gravatar image

updated 2014-08-07 07:16:41 -0500

Hello, I am studying OpenCV and I'm having a lot of fun, but now I'm stuck with a problem where I'm trying to use a background subtraction algorithm to detect any changes.

I followed this tutorial and I managed to get it working to detect changes in a video file (AVI).

The problem I'm having right now is that it tends to incorrectly subtract the background noise and other small changes and instead fill the whole screen pretty much with white.

Here is my implementation of the MOG algorithm on a live camera feed, but the relevant part is this:

    VideoCapture cap;
    if (argc > 1)
        cap.open(argv[1]);
    else
        cap.open(0);
    cap.set(CV_CAP_PROP_FOURCC ,CV_FOURCC('D', 'I', 'V', '3') );
........
    Mat frame, fgMaskMOG;

    Ptr<BackgroundSubtractor> pMOG = new BackgroundSubtractorMOG();
    for (;;)
    {
        if(!cap.read(frame)) {
            cerr << "Unable to read next frame." << endl;
            continue;
        }
        // process the image to obtain a mask image.
        pMOG->operator()(frame, fgMaskMOG);

        std::string time = getDateTime();
        cv::rectangle(frame,cv::Rect(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-25,290,20),cv::Scalar(255,255,255),-1);
        cv::putText(frame,time,cv::Point(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-5),1,1.5,cv::Scalar(0,0,0),2);
........
        // show image.
        imshow("Image", frame);
        imshow("Debug",fgMaskMOG);
        int c = waitKey(30);
        if (c == 'q' || c == 'Q' || (c & 255) == 27)
            break;
    }

This implementation works just fine for a video file as you can see: image description image description
But this is the result when I try to use MOG on a live camera feed: image description image description

(EDIT:

EXPECTED RESULT: My expectation was the same as the video file (see pictures one and two above).
ACTUAL RESULT: The actual result was far from my expected result, much noise was generated (i.e not filtered out), when one put something in front of the camera, it would be black instead of white (inverse from the result of the video file).

- - - - SYSTEM DETAILS - - - -
OS: Windows x64bit
MEMORY AVAILABLE: 3890 MB
WEBCAM: I'm using my built-in webcam on my Satellite C660 TOSHIBA laptop.
COMPILER:
image descriptionMicrosoft Visual Studio Express 2012 for Windows Desktop
image descriptionVersion 11.0.61030.00 Update 4
image descriptionMicrosoft .NET Framework
image descriptionVersion 4.5.50948
OpenCV Version: OpenCV V. 2.4.9, built for Windows, downloaded from SourceForge.
OUTPUT FROM cv::getBuildInformation(): OpenCV_BUILD.txt
Microsoft Visual Studio Project Property Sheet: OpenCV Project Property Sheet
)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-08-05 20:02:51 -0500

updated 2014-08-07 20:09:48 -0500

You might be having memory reference problems, try to clone the frame into its own memory space.

Try this:

cv::Mat frame_clone;
frame.copyTo(frame_clone)
// process the image to obtain a mask image.
pMOG->operator()(frame_clone, fgMaskMOG);

[EDIT]

I tried your code with a video taken with a crappy webcam and the live feed of the same webcam. I get the same result as you: Good with video, bad with live feed.

But I observed something: the images on the recorded video are far more stable (do not change much from frame to frame) than the ones from the live feed (there is a lot of noise from frame to frame).

I think that by requesting the live stream of the webcam you get a highly compressed low quality image, that is causing the MOG algorithm to give you crappy results with the default parameters.

Possible solutions: find a way to increase the quality of the live feed or tweak the parameters of the MOG to deal with the frame-to-frame variability of the live feed.

edit flag offensive delete link more

Comments

Still same result :(

Linus gravatar imageLinus ( 2014-08-06 01:59:12 -0500 )edit

I edited my question to provide more information about my System, would you mind have a look again? I would appreciate it a lot.

Linus gravatar imageLinus ( 2014-08-07 07:15:36 -0500 )edit

I edited my answer, please check it

Martin Peris gravatar imageMartin Peris ( 2014-08-07 20:09:04 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-08-05 16:47:33 -0500

Seen: 1,488 times

Last updated: Aug 07 '14