background subtraction

asked 2020-01-05 11:38:20 -0600

dineshlama gravatar image

updated 2020-01-05 11:38:55 -0600

I was using and testing MOG2 background subtraction and othres provided by opencv. But it looks like thsi function don't takes as still image as a background instead takes it every time. I want to use one single image as a background and than use forground image to be subracted and give the result. Isn't it possible using any opencv function? This is the code i was using to check the mog2 function:

#include <iostream>
#include <sstream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
using namespace cv;
using namespace std;
const char* params
    = "{ help h         |           | Print usage }"
      "{ input          | vtest.avi | Path to a video or a sequence of image }"
      "{ algo           | MOG2      | Background subtraction method (KNN, MOG2) }";
int main(int argc, char* argv[])
{
    CommandLineParser parser(argc, argv, params);
    parser.about( "This program shows how to use background subtraction methods provided by "
                  " OpenCV. You can process both videos and images.\n" );
    if (parser.has("help"))
    {
        //print help information
        parser.printMessage();
    }
    //create Background Subtractor objects
    Ptr<BackgroundSubtractor> pBackSub;
    /*if (parser.get<String>("algo") == "MOG2")
        pBackSub = createBackgroundSubtractorMOG2();
    else
        pBackSub = createBackgroundSubtractorKNN();*/
    pBackSub = createBackgroundSubtractorMOG2();

    VideoCapture cap;
    if(!cap.open(0))
      return 0;

    /*VideoCapture capture( samples::findFile( parser.get<String>("input") ) );
    if (!capture.isOpened()){
        //error in opening the video input
        cerr << "Unable to open: " << parser.get<String>("input") << endl;
        return 0;
    }*/

    Mat frame, fgMask;
    while (true) {
        cap >> frame;
        if (frame.empty())
            break;
        //update the background model
        pBackSub->apply(frame, fgMask);
        //get the frame number and write it on the current frame
        rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                  cv::Scalar(255,255,255), -1);
        stringstream ss;
        ss << cap.get(CAP_PROP_POS_FRAMES);
        string frameNumberString = ss.str();
        putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
                FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
        //show the current frame and the fg masks
        imshow("Frame", frame);
        imshow("FG Mask", fgMask);
        //get the input from the keyboard
        int keyboard = waitKey(30);
        if (keyboard == 'q' || keyboard == 27)
            break;
    }
    return 0;
}
edit retag flag offensive close merge delete

Comments

I want to use one single image as a background and than use forground image to be subracted and give the result

why do you try to use MOG2 then,where a simple absdiff() would do ?

what are you trying to achieve ? what is the purpose of your program ?

berak gravatar imageberak ( 2020-01-05 12:23:55 -0600 )edit
1

What i want is i have a camera taking a background image stream contineuly. Now when new object enters in this scene its pixels and controus should be extracted till it is inside the camera scene. as i used mog2 after few seconds the newly entered object is also considered as background in it and also the contour and the body pixel is not that good. Now, i tried as u suggested absdiff() and did it myself using different color models like lab, luv, hsv etc, but i found that due to noise and other factors the result is also little bit of noisy. i tried noise removal functions like fastNlMeansDenoisingColoredMulti but it is making the frame rate drop drastically where the mog2 has good frame rate without having such noise.

dineshlama gravatar imagedineshlama ( 2020-01-05 23:58:43 -0600 )edit