1 | initial version |
an old question but i recently tried something like
#include "opencv2/videoio.hpp"
#include "opencv2/video.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
const int nmixtures = 10;
const bool shadow = false;
const int history = 1000;
Ptr<BackgroundSubtractor> back_obj = createBackgroundSubtractorMOG2(history, nmixtures, shadow);
int main(int argc, char** argv) {
Mat original, background, foreground;
VideoCapture cap(0);
vector<vector<Point> >contours;
int counter = 0;
double learningRate = -1.0;
while (1)
{
counter++;
if (counter < 5)
learningRate = 1.0;
else
learningRate = counter > 30 ? 0.0 : -1.0;
cap >> original;
back_obj->apply(original, foreground, learningRate);
back_obj->getBackgroundImage(background);
findContours(foreground, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
drawContours(original, contours, -1, Scalar(255, 255, 0), 3);
imshow("ORIGINAL", original);
imshow("BACKGROUND", background);
imshow("FOREGROUND", foreground);
int key = waitKey(1);
if (key == 32)
{
counter = 0;
}
if(key == 27)
break;
}
return 0;
}