/
segment_objects.cpp
138 lines (116 loc) · 3.75 KB
/
segment_objects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include <stdio.h>
#include <string>
using namespace std;
using namespace cv;
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");
}
static void refineSegments(const Mat& img, Mat& mask, Mat& dst)
{
int niters = 3;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat temp;
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);
findContours( temp, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE );
dst = Mat::zeros(img.size(), CV_8UC3);
if( contours.size() == 0 ) {
return;
}
// iterate through all the top-level contours,
// draw each connected component with its own random color
/*
int idx = 0, largestComp = 0;
double maxArea = 0;
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( 255, 255, 255 );
drawContours( dst, contours, largestComp, color, FILLED, LINE_8, hierarchy );
//drawContours( img, contours, largestComp, color, FILLED, LINE_8, hierarchy );
*/
RNG& rng = theRNG();
for (int x = 0; x < contours.size(); x++ ) {
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(dst, contours, x, color, FILLED, LINE_8, hierarchy, 0, Point());
}
vector<Rect> boundRect(contours.size());
for (int x = 0; x < contours.size(); x++) {
RotatedRect box = minAreaRect(contours[x]);
Point2f vertex[4];
box.points(vertex);
for (int y = 0; y < 4; y++) {
line(img, vertex[y], vertex[(y+1)%4], Scalar(0, 255, 0), 2, LINE_AA);
}
}
}
int main(int argc, char** argv)
{
VideoCapture cap;
bool update_bg_model = true;
CommandLineParser parser(argc, argv, "{help h||}{@input||}");
if (parser.has("help"))
{
help();
return 0;
}
string input = parser.get<std::string>("@input");
if (input.empty())
cap.open(0);
else
cap.open(input);
if( !cap.isOpened() )
{
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;
}
namedWindow("video", 1);
namedWindow("segmented", 1);
Ptr<BackgroundSubtractorMOG2> bgsubtractor=createBackgroundSubtractorMOG2();
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("video", tmp_frame);
imshow("segmented", out_frame);
int keycode = waitKey(30);
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;
}