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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* Copyright (c) 2012 Patrick Ruoff
* Copyright (c) 2014-2016 Stanislaw Halik <sthalik@misaki.pl>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
#include "ftnoir_tracker_pt.h"
#include "video/video-widget.hpp"
#include "compat/camera-names.hpp"
#include "compat/math-imports.hpp"
#include "pt-api.hpp"
#include <QHBoxLayout>
#include <QDebug>
#include <QFile>
#include <QCoreApplication>
namespace pt_module {
Tracker_PT::Tracker_PT(pointer<pt_runtime_traits> const& traits) :
traits { traits },
s { traits->get_module_name() },
point_extractor { traits->make_point_extractor() },
camera { traits->make_camera() },
frame { traits->make_frame() },
preview_frame { traits->make_preview(preview_width, preview_height) }
{
cv::setBreakOnError(true);
cv::setNumThreads(1);
connect(s.b.get(), SIGNAL(saving()), this, SLOT(maybe_reopen_camera()), Qt::DirectConnection);
connect(&s.fov, SIGNAL(valueChanged(int)), this, SLOT(set_fov(int)), Qt::DirectConnection);
set_fov(s.fov);
}
Tracker_PT::~Tracker_PT()
{
requestInterruption();
wait();
QMutexLocker l(&camera_mtx);
camera->stop();
}
void Tracker_PT::run()
{
maybe_reopen_camera();
while(!isInterruptionRequested())
{
pt_camera_info info;
bool new_frame = false;
{
QMutexLocker l(&camera_mtx);
if (camera)
std::tie(new_frame, info) = camera->get_frame(*frame);
}
if (new_frame)
{
*preview_frame = *frame;
point_extractor->extract_points(*frame, *preview_frame, points);
point_count = points.size();
const f fx = pt_camera_info::get_focal_length(info.fov, info.res_x, info.res_y);
const bool success = points.size() >= PointModel::N_POINTS;
Affine X_CM;
{
QMutexLocker l(¢er_lock);
if (success)
{
point_tracker.track(points,
PointModel(s),
info,
s.dynamic_pose ? s.init_phase_timeout : 0);
ever_success = true;
}
QMutexLocker l2(&data_lock);
X_CM = point_tracker.pose();
}
Affine X_MH(mat33::eye(), vec3(s.t_MH_x, s.t_MH_y, s.t_MH_z));
Affine X_GH = X_CM * X_MH;
vec3 p = X_GH.t; // head (center?) position in global space
preview_frame->draw_head_center((p[0] * fx) / p[2], (p[1] * fx) / p[2]);
widget->update_image(preview_frame->get_bitmap());
{
int w = -1, h = -1;
widget->get_preview_size(w, h);
if (w != preview_width || h != preview_height)
{
preview_width = w; preview_height = h;
preview_frame = traits->make_preview(w, h);
}
}
}
}
}
bool Tracker_PT::maybe_reopen_camera()
{
QMutexLocker l(&camera_mtx);
return camera->start(camera_name_to_index(s.camera_name),
s.cam_fps, s.cam_res_x, s.cam_res_y);
}
void Tracker_PT::set_fov(int value)
{
QMutexLocker l(&camera_mtx);
camera->set_fov(value);
}
module_status Tracker_PT::start_tracker(QFrame* video_frame)
{
//video_frame->setAttribute(Qt::WA_NativeWindow);
widget = std::make_unique<video_widget>(video_frame);
layout = std::make_unique<QHBoxLayout>(video_frame);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(widget.get());
video_frame->setLayout(layout.get());
//video_widget->resize(video_frame->width(), video_frame->height());
video_frame->show();
start(QThread::HighPriority);
return {};
}
void Tracker_PT::data(double *data)
{
if (ever_success)
{
Affine X_CM;
{
QMutexLocker l(&data_lock);
X_CM = point_tracker.pose();
}
Affine X_MH(mat33::eye(), vec3(s.t_MH_x, s.t_MH_y, s.t_MH_z));
Affine X_GH(X_CM * X_MH);
// translate rotation matrix from opengl (G) to roll-pitch-yaw (E) frame
// -z -> x, y -> z, x -> -y
mat33 R_EG(0, 0,-1,
-1, 0, 0,
0, 1, 0);
mat33 R(R_EG * X_GH.R * R_EG.t());
// get translation(s)
const vec3& t = X_GH.t;
// extract rotation angles
double alpha, beta, gamma;
beta = atan2( (double)-R(2,0), (double)sqrt(R(2,1)*R(2,1) + R(2,2)*R(2,2)) );
alpha = atan2( (double)R(1,0), (double)R(0,0) );
gamma = atan2( (double)R(2,1), (double)R(2,2) );
constexpr double rad2deg = 180/M_PI;
data[Yaw] = rad2deg * alpha;
data[Pitch] = -rad2deg * beta;
data[Roll] = rad2deg * gamma;
// convert to cm
data[TX] = (double)t[0] / 10;
data[TY] = (double)t[1] / 10;
data[TZ] = (double)t[2] / 10;
}
}
bool Tracker_PT::center()
{
QMutexLocker l(¢er_lock);
point_tracker.reset_state();
return false;
}
int Tracker_PT::get_n_points()
{
return int(point_count);
}
bool Tracker_PT::get_cam_info(pt_camera_info& info)
{
QMutexLocker l(&camera_mtx);
bool ret;
std::tie(ret, info) = camera->get_info();
return ret;
}
Affine Tracker_PT::pose() const
{
QMutexLocker l(&data_lock);
return point_tracker.pose();
}
} // ns pt_module
|