-
Notifications
You must be signed in to change notification settings - Fork 71
/
GazeTracker.cpp
166 lines (137 loc) · 4.87 KB
/
GazeTracker.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
#include "GazeTracker.h"
int Targets::getCurrentTarget(Point point) {
vector<double> distances(targets.size());
// debugtee(targets);
transform(targets.begin(), targets.end(), distances.begin(),
sigc::mem_fun(point, &Point::distance));
// debugtee(distances);
return min_element(distances.begin(), distances.end()) - distances.begin();
// for(int i=0; i<targets.size(); i++)
// if (point.distance(targets[i]) < 30)
// return i;
// return -1;
}
CalTarget::CalTarget() {}
CalTarget::CalTarget(Point point,
const IplImage* image, const IplImage* origimage):
point(point),
image(cvCloneImage(image), releaseImage),
origimage(cvCloneImage(origimage), releaseImage)
{
}
void CalTarget::save(CvFileStorage* out, const char* name) {
cvStartWriteStruct(out, name, CV_NODE_MAP);
point.save(out, "point");
cvWrite(out, "image", image.get());
cvWrite(out, "origimage", origimage.get());
cvEndWriteStruct(out);
}
void CalTarget::load(CvFileStorage* in, CvFileNode *node) {
point.load(in, cvGetFileNodeByName(in, node, "point"));
image.reset((IplImage*) cvReadByName(in, node, "image"));
origimage.reset((IplImage*) cvReadByName(in, node, "origimage"));
}
TrackerOutput::TrackerOutput(Point gazepoint, Point target, int targetid):
gazepoint(gazepoint), target(target), targetid(targetid)
{
}
template <class T, class S>
vector<S> getsubvector(vector<T> const& input, S T::*ptr) {
vector<S> output(input.size());
for(int i=0; i<input.size(); i++)
output[i] = input[i].*ptr;
return output;
}
double GazeTracker::imagedistance(const IplImage *im1, const IplImage *im2) {
double norm = cvNorm(im1, im2, CV_L2);
return norm*norm;
}
double GazeTracker::covariancefunction(SharedImage const& im1,
SharedImage const& im2)
{
const double sigma = 1.0;
const double lscale = 500.0;
return sigma*sigma*exp(-imagedistance(im1.get(),im2.get())/(2*lscale*lscale));
}
void GazeTracker::updateGPs(void) {
Vector xlabels(caltargets.size());
Vector ylabels(caltargets.size());
for(int i=0; i<caltargets.size(); i++) {
xlabels[i] = caltargets[i].point.x;
ylabels[i] = caltargets[i].point.y;
}
vector<SharedImage> images =
getsubvector(caltargets, &CalTarget::image);
gpx.reset(new ImProcess(images, xlabels, covariancefunction, 0.01));
gpy.reset(new ImProcess(images, ylabels, covariancefunction, 0.01));
targets.reset(new Targets(getsubvector(caltargets, &CalTarget::point)));
}
void GazeTracker::clear() {
caltargets.clear();
// updateGPs()
}
void GazeTracker::addExemplar(Point point,
const IplImage *eyefloat,
const IplImage *eyegrey)
{
caltargets.push_back(CalTarget(point, eyefloat, eyegrey));
updateGPs();
}
// void GazeTracker::updateExemplar(int id,
// const IplImage *eyefloat,
// const IplImage *eyegrey)
// {
// cvConvertScale(eyegrey, caltargets[id].origimage.get());
// cvAdd(caltargets[id].image.get(), eyefloat, caltargets[id].image.get());
// cvConvertScale(caltargets[id].image.get(), caltargets[id].image.get(), 0.5);
// updateGPs();
// }
void GazeTracker::draw(IplImage *destimage, int eyedx, int eyedy) {
// for(int i=0; i<caltargets.size(); i++) {
// Point p = caltargets[i].point;
// cvSetImageROI(destimage, cvRect((int)p.x - eyedx, (int)p.y - eyedy,
// 2*eyedx, 2*eyedy));
// cvCvtColor(caltargets[i].origimage, destimage, CV_GRAY2RGB);
// cvRectangle(destimage, cvPoint(0,0), cvPoint(2*eyedx-1,2*eyedy-1),
// CV_RGB(255,0,255));
// }
// cvResetImageROI(destimage);
}
void GazeTracker::save(void) {
CvFileStorage *out =
cvOpenFileStorage("calibration.xml", NULL, CV_STORAGE_WRITE);
save(out, "GazeTracker");
cvReleaseFileStorage(&out);
}
void GazeTracker::save(CvFileStorage *out, const char *name) {
cvStartWriteStruct(out, name, CV_NODE_MAP);
savevector(out, "caltargets", caltargets);
cvEndWriteStruct(out);
}
void GazeTracker::load(void) {
CvFileStorage *in =
cvOpenFileStorage("calibration.xml", NULL, CV_STORAGE_READ);
CvFileNode *root = cvGetRootFileNode(in);
load(in, cvGetFileNodeByName(in, root, "GazeTracker"));
cvReleaseFileStorage(&in);
updateGPs();
}
void GazeTracker::load(CvFileStorage *in, CvFileNode *node) {
caltargets = loadvector<CalTarget>(in, cvGetFileNodeByName(in, node,
"caltargets"));
}
static void ignore(const IplImage *) {}
void GazeTracker::update(const IplImage *image) {
if (isActive()) {
output.gazepoint = Point(gpx->getmean(SharedImage(image, &ignore)),
gpy->getmean(SharedImage(image, &ignore)));
output.targetid = getTargetId(output.gazepoint);
output.target = getTarget(output.targetid);
}
}
int GazeTracker::getTargetId(Point point) {
return targets->getCurrentTarget(point);
}
Point GazeTracker::getTarget(int id) {
return targets->targets[id];
}