-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_track.cpp
174 lines (115 loc) · 4.22 KB
/
video_track.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "video_track.h"
using namespace cv;
using namespace std;
#define getMillis() std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()
struct traceble
{vector<Point2f> tracKeyp;
Rect tracRect;
bool isTracked = false;
int lifeTime = 0;
};
int main(int argc, char **argv) {
Mat frame, gray, oldFrame, grayframe(Size(0,0),CV_8UC1), grayoldFrame(Size(0,0),CV_8UC1), grayold;
if (argc != 3 )
{
printf("usage: ./filename <Video> <path_directory>\n");
return -1;
}
String filename = argv[1];
video_track trck_vd = video_track();
VideoCapture video(filename); //open video given by command line
if (!video.isOpened()) {
cout << "Couldn't find or open the video!\n" << endl;
return -1;
}
String input_directoryKM = argv[2];
//classifier for boat detection
KMeansClassifier classifier(0.9);
classifier.load(input_directoryKM,true);
video.read(frame);
//preparing output video
VideoWriter outvideo = trck_vd.prep_video(video,filename);
oldFrame = frame.clone();
vector<traceble> tracVect;
int countFrame = 0;
int limitFrame = 3;
unsigned long ms;
while(video.read(frame)) {
ms = getMillis();
vector<Rect> outBB;
int num_boats;
//some preprocessing for every frame
gray = trck_vd.preproc(frame);
grayold = trck_vd.preproc(oldFrame);
if(countFrame>limitFrame)
outBB = trck_vd.findBBMovement(grayold,gray);
if(!tracVect.empty()) {
cvtColor(oldFrame, grayoldFrame, COLOR_BGR2GRAY);
cvtColor(frame, grayframe, COLOR_BGR2GRAY);
for (int k = 0; k < tracVect.size(); k++) {
Point2f delta;
tracVect[k].tracKeyp = trck_vd.track(grayoldFrame, grayframe, tracVect[k].tracKeyp, delta);
tracVect[k].isTracked = true;
tracVect[k].tracRect.x = tracVect[k].tracRect.x + delta.x;
tracVect[k].tracRect.y = tracVect[k].tracRect.y + delta.y;
if(norm(delta) < 2)
tracVect[k].lifeTime = 10;
}
tracVect.erase(std::remove_if(tracVect.begin(), tracVect.end(), [](const traceble& f) {
return (f.tracKeyp.size() < 3) || f.lifeTime >= 10;
}), tracVect.end());
}
drawROIs(frame, outBB);
for (int i = 0; i < outBB.size(); i++) {
int sizeROI;
double maxIoU = -1;
int maxIndex;
double minArea;
for(int j = 0; j < tracVect.size() ; j++)
{
minArea = outBB[i].area();
if(minArea > tracVect[j].tracRect.area())
minArea = tracVect[j].tracRect.area();
double IoU = ((outBB[i] & tracVect[j].tracRect).area())/minArea;
if(IoU > maxIoU)
{ maxIoU = IoU;
maxIndex = j;}
}
if(maxIoU < 0.5) {
vector<Point2f> kp;
num_boats = trck_vd.checkBoats(outBB[i], frame, classifier, sizeROI,kp);
if (num_boats > (sizeROI / 6)) {
traceble temp ;
temp.tracKeyp = kp;
temp.tracRect = outBB[i];
tracVect.push_back(temp);
}
}
else
{
tracVect[maxIndex].tracRect |= outBB[i];
}
}
//finally draw the rects found
for(const auto &tr : tracVect)
{
if(tr.isTracked)
rectangle(frame,tr.tracRect,Scalar(0,255,0),2);
}
//show on a window the video frame by frame
imshow("Camera", frame);
outvideo.write(frame);
unsigned long delta = getMillis()-ms;
delta = (delta>=100)? 1 : (100-delta);
char c = (char) cv::waitKey(delta);
if(c == 27){
//exit if ESC is pressed
break;
}
if(countFrame > limitFrame)
{ oldFrame = frame.clone();
countFrame = 0;}
countFrame++;
}
return 0;
}