2016-02-15 21:37:29 +08:00
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
#include "opencv2/videoio.hpp"
|
|
|
|
#include "opencv2/highgui.hpp"
|
2011-05-22 02:32:34 +08:00
|
|
|
#include "opencv2/video/background_segm.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
using namespace std;
|
2010-05-12 01:44:00 +08:00
|
|
|
using namespace cv;
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2010-12-01 09:14:16 +08:00
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
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");
|
2010-12-01 09:14:16 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void refineSegments(const Mat& img, Mat& mask, Mat& dst)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int niters = 3;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
vector<vector<Point> > contours;
|
|
|
|
vector<Vec4i> hierarchy;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
Mat temp;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
dilate(mask, temp, Mat(), Point(-1,-1), niters);
|
|
|
|
erode(temp, temp, Mat(), Point(-1,-1), niters*2);
|
|
|
|
dilate(temp, temp, Mat(), Point(-1,-1), niters);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2013-04-06 22:16:51 +08:00
|
|
|
findContours( temp, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE );
|
2012-06-08 01:21:29 +08:00
|
|
|
|
|
|
|
dst = Mat::zeros(img.size(), CV_8UC3);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( contours.size() == 0 )
|
|
|
|
return;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// iterate through all the top-level contours,
|
|
|
|
// draw each connected component with its own random color
|
|
|
|
int idx = 0, largestComp = 0;
|
|
|
|
double maxArea = 0;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( ; idx >= 0; idx = hierarchy[idx][0] )
|
|
|
|
{
|
|
|
|
const vector<Point>& c = contours[idx];
|
|
|
|
double area = fabs(contourArea(Mat(c)));
|
|
|
|
if( area > maxArea )
|
|
|
|
{
|
|
|
|
maxArea = area;
|
|
|
|
largestComp = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Scalar color( 0, 0, 255 );
|
2013-04-08 02:45:38 +08:00
|
|
|
drawContours( dst, contours, largestComp, color, FILLED, LINE_8, hierarchy );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
VideoCapture cap;
|
|
|
|
bool update_bg_model = true;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-08-01 23:24:23 +08:00
|
|
|
CommandLineParser parser(argc, argv, "{help h||}{@input||}");
|
|
|
|
if (parser.has("help"))
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
string input = parser.get<std::string>("@input");
|
|
|
|
if (input.empty())
|
2010-05-12 01:44:00 +08:00
|
|
|
cap.open(0);
|
|
|
|
else
|
2015-08-01 23:24:23 +08:00
|
|
|
cap.open(input);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( !cap.isOpened() )
|
|
|
|
{
|
2010-12-01 09:14:16 +08:00
|
|
|
printf("\nCan not open camera or video file\n");
|
2010-05-12 01:44:00 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
Mat tmp_frame, bgmask, out_frame;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
cap >> tmp_frame;
|
2014-08-13 19:08:27 +08:00
|
|
|
if(tmp_frame.empty())
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
printf("can not read data from the video source\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
namedWindow("video", 1);
|
|
|
|
namedWindow("segmented", 1);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2014-08-11 03:24:16 +08:00
|
|
|
Ptr<BackgroundSubtractorMOG2> bgsubtractor=createBackgroundSubtractorMOG2();
|
|
|
|
bgsubtractor->setVarThreshold(10);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
cap >> tmp_frame;
|
2014-08-13 19:08:27 +08:00
|
|
|
if( tmp_frame.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2013-03-20 23:51:49 +08:00
|
|
|
bgsubtractor->apply(tmp_frame, bgmask, update_bg_model ? -1 : 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
refineSegments(tmp_frame, bgmask, out_frame);
|
|
|
|
imshow("video", tmp_frame);
|
|
|
|
imshow("segmented", out_frame);
|
2010-07-17 18:35:17 +08:00
|
|
|
int keycode = waitKey(30);
|
2010-05-12 01:44:00 +08:00
|
|
|
if( keycode == 27 )
|
|
|
|
break;
|
|
|
|
if( keycode == ' ' )
|
2010-12-01 09:14:16 +08:00
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
update_bg_model = !update_bg_model;
|
2010-12-01 09:14:16 +08:00
|
|
|
printf("Learn background is in state = %d\n",update_bg_model);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|