#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include <stdio.h>
#include <string>
static void help()
{
printf("\n"
"This program demonstrated a simple method of connected components clean up of background subtraction\n"
"When the program starts, it begins learning the background.\n"
"You can toggle background learning on and off by hitting the space bar.\n"
"Call\n"
"./segment_objects [video file, else it reads camera 0]\n\n");
}
{
int niters = 3;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
if( contours.size() == 0 )
return;
int idx = 0, largestComp = 0;
double maxArea = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
const vector<Point>& c = contours[idx];
if( area > maxArea )
{
maxArea = area;
largestComp = idx;
}
}
}
int main(int argc, char** argv)
{
bool update_bg_model = true;
{
help();
return 0;
}
string input = parser.
get<std::string>(
"@input");
if (input.empty())
else
{
printf("\nCan not open camera or video file\n");
return -1;
}
Mat tmp_frame, bgmask, out_frame;
cap >> tmp_frame;
if(tmp_frame.empty())
{
printf("can not read data from the video source\n");
return -1;
}
bgsubtractor->setVarThreshold(10);
for(;;)
{
cap >> tmp_frame;
if( tmp_frame.empty() )
break;
bgsubtractor->apply(tmp_frame, bgmask, update_bg_model ? -1 : 0);
refineSegments(tmp_frame, bgmask, out_frame);
imshow(
"segmented", out_frame);
if( keycode == 27 )
break;
if( keycode == ' ' )
{
update_bg_model = !update_bg_model;
printf("Learn background is in state = %d\n",update_bg_model);
}
}
return 0;
}