0

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
(source: opencv.org)

image description
(source: opencv.org)


But this is the result when I try to use MOG on a live camera feed: image description
(source: opencv.org)

image description
(source: opencv.org)


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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Linus
  • 1,516
  • 17
  • 35
  • This is probably because of a lot of noise. And camera movement? Anyway it is a good idea to use a Gaussian filter. And maybe adjust the learning rate a bit? – a-Jays Aug 06 '14 at 13:15
  • @a-Jays There's no camera movement, and there's no major difference between the live camera feed and the already recorded video. It works just fine for the already recorded video, so why should it make such a difference? Anyhow, what do you mean by adjusting the learning rate, what are you suggesting? – Linus Aug 06 '14 at 13:32
  • may it it helps you : http://stackoverflow.com/questions/16808838/background-and-foreground-in-opencv – Engine Aug 06 '14 at 13:40
  • When do you stop updating background model? In the first image pair it seems you have a well updated background model and in the second image pair it seems you have stopped updated background model early and somehow reversed the background with foreground. I need to see your full code but I cannot reach pastebin can you put it another place? – guneykayim Aug 06 '14 at 14:23
  • @guneykayim Yes, I've put it on [Dropbox](https://www.dropbox.com/s/7gc44ntsv3sbi7c/motion_cap.cpp) :) – Linus Aug 06 '14 at 14:26
  • Only difference you do is just setting camera or avi file and these are the results. Are you 100 % sure that you are not changing anything else? – guneykayim Aug 06 '14 at 14:34
  • @guneykayim Yes, I pass the video file name as a parameter if I want the background subtraction on the video. – Linus Aug 06 '14 at 14:38
  • Can you try it one more time using different camera and avi file? – guneykayim Aug 06 '14 at 14:39
  • @guneykayim I tried it once more, but now with a smooth white wall as the background, when I moved something in front of the camera, it generated A LOT of noise, similar to what you see on the images above. (**EDIT:** I think I misunderstood your last comment, do you want me to try it on another laptop with a different webcam?) – Linus Aug 06 '14 at 14:47

0 Answers0