forked from tiendan/OpenGazer
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalibrator.cpp
More file actions
Latest commit
172 lines (139 loc) · 4.55 KB
/
Copy pathCalibrator.cpp
File metadata and controls
172 lines (139 loc) · 4.55 KB
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
#include"Calibrator.h"
#include"Application.h"
Calibrator::Calibrator(const std::vector<Point> &points) :
_window(2, false) // Create this window in the second screen
{
_points = points;
// Clear gaze tracker components' calibration info
for (int i=0; i<Application::config.gaze_components.size(); i++){
GazeTrackerComponent* comp = (GazeTrackerComponent*) Application::getComponent(Application::config.gaze_components[i]);
comp->clear();
}
_frameNumber = -1;
needRecalibration = false;
_screenImage.create(cv::Size(_window.size().width(), _window.size().height()), CV_8UC3);
_targetImage = cv::imread("./target.png", CV_LOAD_IMAGE_COLOR);
_window.setWindowTitle("Opengazer");
}
Calibrator::~Calibrator() {}
// Main processing function
voidCalibrator::process() {
if(Application::Signals::initiateCalibrationFrameNo == Application::Components::videoInput->frameCount) {
start();
}
if(!isActive())
return;
needRecalibration = false;
// Increment the frame counter
_frameNumber++;
if (shouldStartNextPoint()) {
pointStart();
} else {
if (isLastFrame()) {
pointEnd();
}
if (isFinished()) {
calibrationEnded();
}
}
}
// Start calibration procedure. Reset frame counter and save target point
// locations
voidCalibrator::start() {
_frameNumber = 0;
_window.show();
Application::Data::calibrationTargets.clear();
Application::status = Application::STATUS_CALIBRATING;
}
// Displays the next target on screen
voidCalibrator::pointStart() {
// Get the current target
Point target = getActivePoint();
// Calculate bounds for copying target image to screen image
cv::Rect targetBounds = cv::Rect(0, 0, _targetImage.size().width, _targetImage.size().height);
cv::Rect screenBounds = cv::Rect(target.x - _targetImage.size().width/2, target.y - _targetImage.size().height/2, _targetImage.size().width, _targetImage.size().height);
// Check bounds
if (screenBounds.x < 0) {
screenBounds.width += screenBounds.x; // Remove the amount from the width
targetBounds.x -= screenBounds.x;
targetBounds.width += screenBounds.x;
screenBounds.x = 0;
}
if (screenBounds.y < 0) {
screenBounds.height += screenBounds.y; // Remove the amount from the height
targetBounds.y -= screenBounds.y;
targetBounds.height += screenBounds.y;
screenBounds.y = 0;
}
if (screenBounds.width + screenBounds.x > _screenImage.size().width) {
screenBounds.width = _screenImage.size().width - screenBounds.x;
}
if (screenBounds.height + screenBounds.y > _screenImage.size().height) {
screenBounds.height = _screenImage.size().height - screenBounds.y;
}
// Fill the screen image with solid color and copy the target image
_screenImage.setTo(cv::Scalar(153, 75, 75));
_targetImage(targetBounds).copyTo(_screenImage(screenBounds));
_window.showImage(_screenImage);
}
voidCalibrator::pointEnd() {
Application::Data::calibrationTargets.push_back(getActivePoint());
needRecalibration = true;
}
// Aborts calibration
voidCalibrator::abortCalibration() {
_window.hide();
_frameNumber = -1;
}
voidCalibrator::calibrationEnded() {
_window.hide();
_frameNumber = -1;
Application::status = Application::STATUS_CALIBRATED;
Application::isTrackerCalibrated = true;
}
// Checks if calibrator is active
boolCalibrator::isActive() {
return _frameNumber >= 0;
}
// Returns current point's coordinates
Point Calibrator::getActivePoint() {
if(isActive())
return _points[getPointNumber()];
else
returnPoint(-1, -1);
}
// Returns the index of current point
intCalibrator::getPointNumber() {
if(isActive())
return (_frameNumber-1) / Application::dwelltimeParameter;
else
return -1;
}
// Returns the frame number for the current point
intCalibrator::getPointFrameNo() {
return (_frameNumber-1) % Application::dwelltimeParameter;
}
//
boolCalibrator::isLastFrame() {
returngetPointFrameNo() == (Application::dwelltimeParameter-1);
}
// Checks if current point is the last
boolCalibrator::isLastPoint() {
returngetPointNumber() == (_points.size() - 1);
}
// Checks if calibration is finished
boolCalibrator::isFinished() {
returngetPointNumber() >= _points.size();
}
// Decides whether calibrator should switch to next point (display it on screen)
boolCalibrator::shouldStartNextPoint() {
return (getPointFrameNo() == 0) && !isFinished();
}
voidCalibrator::draw() {
if(isActive()) {
Point activePoint = getActivePoint();
cv::circle(Application::Components::videoInput->debugFrame,
Utils::mapFromSecondMonitorToDebugFrameCoordinates(cv::Point(activePoint.x, activePoint.y)),
8, cv::Scalar(0, 0, 255), -1, 8, 0);
}
}