-
Notifications
You must be signed in to change notification settings - Fork 71
/
PointTracker.cpp
188 lines (159 loc) · 5.66 KB
/
PointTracker.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "PointTracker.h"
#include "FaceDetector.h"
#include <opencv/highgui.h>
#include <fstream>
PointTracker::PointTracker(const CvSize &size):
flags(CV_LKFLOW_INITIAL_GUESSES),
grey(cvCreateImage(size, 8, 1)),
orig_grey(cvCreateImage(size, 8, 1)),
pyramid(cvCreateImage(size, 8, 1)),
orig_pyramid(cvCreateImage(size, 8, 1)),
last_grey(cvCreateImage(size, 8, 1)),
last_pyramid(cvCreateImage(size, 8, 1))
// origpoints(new CvPoint2D32f[MAX_COUNT]),
// currentpoints(new CvPoint2D32f[MAX_COUNT]),
// status(new char[MAX_COUNT]),
{}
static Point pointbetweenrects(const Point &point, CvRect source, CvRect dest) {
return Point((point.x-source.x)*(double(dest.width)/source.width)+dest.x,
(point.y-source.y)*(double(dest.height)/source.height)+dest.y);
}
static vector<Point> pointbetweenrects(const vector<Point> &points,
CvRect source, CvRect dest)
{
vector<Point> result;
result.reserve(points.size());
xforeach(iter, points)
result.push_back(pointbetweenrects(*iter, source, dest));
return result;
}
void PointTracker::save(string filename, string newpoints,
const IplImage *frame)
{
vector<CvRect> faces = FaceDetector::facedetector.detect(frame);
if (faces.size() == 1) {
cvSaveImage((filename + "-orig-grey.png").c_str(), orig_grey.get());
cvSaveImage((filename + "-orig-pyramid.png").c_str(), orig_pyramid.get());
ofstream origfile((filename + "-orig-points.txt").c_str());
origfile << origpoints;
CvRect face = faces[0];
ofstream facefile(newpoints.c_str());
vector<Point> temppoints;
convert(currentpoints, temppoints);
facefile << pointbetweenrects(temppoints, face, cvRect(0, 0, 1, 1));
}
else
throw ios_base::failure("No face found in the image");
}
void PointTracker::load(string filename, string newpoints,
const IplImage *frame)
{
vector<CvRect> faces = FaceDetector::facedetector.detect(frame);
if (faces.size() == 1) {
ifstream origfile((filename + "-orig-points.txt").c_str());
ifstream facefile(newpoints.c_str());
if (!origfile.is_open() || !facefile.is_open())
throw ios_base::failure("File not found");
// todo: memory leak here, change to scoped_ptr!
orig_grey.reset(cvLoadImage((filename + "-orig-grey.png").c_str(), 0));
orig_pyramid.reset(cvLoadImage((filename + "-orig-pyramid.png").c_str(), 0));
vector<Point> temppoints;
origfile >> temppoints;
convert(temppoints, origpoints);
facefile >> temppoints;
temppoints = pointbetweenrects(temppoints, cvRect(0,0,1,1), faces[0]);
convert(temppoints, currentpoints);
lastpoints = currentpoints;
}
else
throw ios_base::failure("No face found in the image");
}
int PointTracker::getClosestTracker(const Point &point) {
vector<Point> points;
convert(currentpoints, points);
return point.closestPoint(points);
}
void PointTracker::removetracker(int id) {
currentpoints.erase(currentpoints.begin()+id);
lastpoints.erase(lastpoints.begin()+id);
origpoints.erase(origpoints.begin()+id);
}
void PointTracker::synchronizepoints() {
swap(orig_grey, grey);
swap(orig_pyramid, pyramid);
origpoints = lastpoints = currentpoints;
}
void PointTracker::updatetracker(int id, const Point &point) {
currentpoints[id] = point.cvpoint32();
synchronizepoints();
}
void PointTracker::addtracker(const Point &point) {
currentpoints.push_back(point.cvpoint32());
synchronizepoints();
}
void PointTracker::cleartrackers() {
currentpoints.clear();
synchronizepoints();
}
void PointTracker::track(const IplImage *frame, int pyramiddepth)
{
assert(lastpoints.size() == currentpoints.size());
assert(origpoints.size() == currentpoints.size());
status.resize(currentpoints.size());
cvCvtColor(frame, grey.get(), CV_BGR2GRAY );
if (!currentpoints.empty()) {
// first calculate the new position of the features based
// on the (pyramidal) last frame and position estimations
cvCalcOpticalFlowPyrLK(last_grey.get(), grey.get(),
last_pyramid.get(), pyramid.get(),
&lastpoints[0], ¤tpoints[0], pointcount(),
cvSize(win_size,win_size), 2, &status[0], 0,
cvTermCriteria(CV_TERMCRIT_ITER|
CV_TERMCRIT_EPS,20,0.01),
flags);
// then calculate the position based on the original
// template without any pyramids
cvCalcOpticalFlowPyrLK(orig_grey.get(), grey.get(),
orig_pyramid.get(), pyramid.get(),
&origpoints[0], ¤tpoints[0], pointcount(),
cvSize(win_size, win_size),
pyramiddepth, &status[0], 0,
cvTermCriteria(CV_TERMCRIT_ITER|
CV_TERMCRIT_EPS,20,0.01),
flags);
flags |= CV_LKFLOW_PYR_A_READY;
}
cvCopy(grey.get(), last_grey.get(), 0);
cvCopy(pyramid.get(), last_pyramid.get(), 0);
lastpoints = currentpoints;
}
int PointTracker::countactivepoints(void) {
return count_if(status.begin(), status.end(),
bind1st(not_equal_to<char>(), 0));
}
bool PointTracker::areallpointsactive(void) {
return count(status.begin(), status.end(), 0) == 0;
}
void PointTracker::draw(IplImage *canvas) {
for(int i=0; i< (int) currentpoints.size(); i++)
cvCircle( canvas, cvPointFrom32f(currentpoints[i]), 3,
status[i]?(i == eyepoint1 || i == eyepoint2 ?
CV_RGB(255,0,0):
CV_RGB(0,255,0)):
CV_RGB(0,0,255),
-1, 8,0);
}
int PointTracker::pointcount() {
return currentpoints.size();
}
vector<HomPoint>
PointTracker::getpoints(const vector<CvPoint2D32f> PointTracker::*points,
bool allpoints)
{
vector<HomPoint> vec;
for(int i=0; i<pointcount(); i++)
if (allpoints || status[i])
vec.push_back(HomPoint((this->*points)[i].x,
(this->*points)[i].y));
return vec;
}