Skip to content

Commit b89583f

Browse files
committed
add first part of actual mapping (LUT generation)
1 parent ab9425d commit b89583f

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

examples/protonect/include/libfreenect2/registration.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ class LIBFREENECT2_API Registration
4141

4242
private:
4343
void undistort_depth(float dx, float dy, float& mx, float& my);
44+
void depth_to_color(float mx, float my, float& rx, float& ry);
45+
46+
protocol::DepthCameraParamsResponse *depth;
47+
protocol::RgbCameraParamsResponse *color;
4448

45-
float depth_k1, depth_k2, depth_k3;
4649
float undistort_map[512][424][2];
50+
float depth_to_color_map[512][424][2];
4751
};
4852

4953
} /* namespace libfreenect2 */

examples/protonect/src/registration.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
namespace libfreenect2
3131
{
3232

33-
/*
33+
/*
3434
* most information, including the table layout in command_response.h, was
3535
* provided by @sh0 in https://github.com/OpenKinect/libfreenect2/issues/41
3636
*/
@@ -42,49 +42,48 @@ static const float color_q = 0.002199;
4242
void Registration::undistort_depth(float dx, float dy, float& mx, float& my)
4343
{
4444
float ps = (dx * dx) + (dy * dy);
45-
float qs = ((ps * depth_k3 + depth_k2) * ps + depth_k1) * ps + 1.0;
45+
float qs = ((ps * depth->k3 + depth->k2) * ps + depth->k1) * ps + 1.0;
4646
for (int i = 0; i < 9; i++) {
4747
float qd = ps / (qs * qs);
48-
qs = ((qd * depth_k3 + depth_k2) * qd + depth_k1) * qd + 1.0;
48+
qs = ((qd * depth->k3 + depth->k2) * qd + depth->k1) * qd + 1.0;
4949
}
5050
mx = dx / qs;
5151
my = dy / qs;
5252
}
5353

54-
/*void kinect2_depth_to_color(float mx, float my, float z, float& rx, float& ry)
54+
void Registration::depth_to_color(float mx, float my, float& rx, float& ry)
5555
{
56-
mx *= depth_f * depth_q;
57-
my *= depth_f * depth_q;
56+
mx *= depth->fx * depth_q;
57+
my *= depth->fy * depth_q;
5858

59-
float wx =
60-
(mx * mx * mx * mx_x3y0) + (my * my * my * mx_x0y3) +
61-
(mx * mx * my * mx_x2y1) + (my * my * mx * mx_x1y2) +
62-
(mx * mx * mx_x2y0) + (my * my * mx_x0y2) + (mx * my * mx_x1y1) +
63-
(mx * mx_x1y0) + (my * mx_x0y1) + (mx_x0y0);
64-
float wy =
65-
(mx * mx * mx * my_x3y0) + (my * my * my * my_x0y3) +
66-
(mx * mx * my * my_x2y1) + (my * my * mx * my_x1y2) +
67-
(mx * mx * my_x2y0) + (my * my * my_x0y2) + (mx * my * my_x1y1) +
68-
(mx * my_x1y0) + (my * my_x0y1) + (my_x0y0);
59+
float wx =
60+
(mx * mx * mx * color->mx_x3y0) + (my * my * my * color->mx_x0y3) +
61+
(mx * mx * my * color->mx_x2y1) + (my * my * mx * color->mx_x1y2) +
62+
(mx * mx * color->mx_x2y0) + (my * my * color->mx_x0y2) + (mx * my * color->mx_x1y1) +
63+
(mx * color->mx_x1y0) + (my * color->mx_x0y1) + (color->mx_x0y0);
6964

70-
rx = wx / (color_f * color_q);
71-
ry = wy / (color_f * color_q);
65+
float wy =
66+
(mx * mx * mx * color->my_x3y0) + (my * my * my * color->my_x0y3) +
67+
(mx * mx * my * color->my_x2y1) + (my * my * mx * color->my_x1y2) +
68+
(mx * mx * color->my_x2y0) + (my * my * color->my_x0y2) + (mx * my * color->my_x1y1) +
69+
(mx * color->my_x1y0) + (my * color->my_x0y1) + (color->my_x0y0);
7270

73-
rx += (shift_m / z) - (shift_m / shift_d);
71+
rx = wx / (color->color_f * color_q);
72+
ry = wy / (color->color_f * color_q);
73+
}
74+
/*
75+
rx += (depth->shift_m / z) - (depth->shift_m / depth->shift_d);
7476
75-
rx = rx * color_f + color_cx;
76-
ry = ry * color_f + color_cy;
77+
rx = rx * color->fx + color_cx;
78+
ry = ry * color->fy + color_cy;
7779
}*/
7880

79-
Registration::Registration(protocol::DepthCameraParamsResponse *depth_p, protocol::RgbCameraParamsResponse *rgb_p)
81+
Registration::Registration(protocol::DepthCameraParamsResponse *depth_p, protocol::RgbCameraParamsResponse *rgb_p):
82+
depth(depth_p), color(rgb_p)
8083
{
8184
float mx, my;
8285
int rx, ry;
8386

84-
depth_k1 = depth_p->k1;
85-
depth_k2 = depth_p->k2;
86-
depth_k3 = depth_p->k3;
87-
8887
for (int x = 0; x < 512; x++)
8988
for (int y = 0; y < 424; y++) {
9089
undistort_depth(x,y,mx,my);
@@ -93,6 +92,15 @@ Registration::Registration(protocol::DepthCameraParamsResponse *depth_p, protoco
9392
undistort_map[rx][ry][0] = x;
9493
undistort_map[rx][ry][1] = y;
9594
}
95+
96+
for (int x = 0; x < 512; x++)
97+
for (int y = 0; y < 424; y++) {
98+
depth_to_color(x,y,mx,my);
99+
rx = round(mx);
100+
ry = round(my);
101+
depth_to_color_map[rx][ry][0] = x;
102+
depth_to_color_map[rx][ry][1] = y;
103+
}
96104
}
97105

98106
} /* namespace libfreenect2 */

0 commit comments

Comments
 (0)