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