Skip to content

Commit 2c586d3

Browse files
committed
Merge pull request #190 from floe/registration
add basic Registration class based on information by @sh0
2 parents baa6929 + cc6fc49 commit 2c586d3

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed

examples/protonect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ SET(SOURCES
8787

8888
src/usb_control.cpp
8989
src/command_transaction.cpp
90+
src/registration.cpp
9091
src/libfreenect2.cpp
9192
${LIBFREENECT2_THREADING_SOURCE}
9293
${RESOURCES_INC_FILE}

examples/protonect/include/libfreenect2/libfreenect2.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ class LIBFREENECT2_API Freenect2Device
4545
struct ColorCameraParams
4646
{
4747
float fx, fy, cx, cy;
48+
49+
float shift_d, shift_m;
50+
51+
float mx_x3y0; // xxx
52+
float mx_x0y3; // yyy
53+
float mx_x2y1; // xxy
54+
float mx_x1y2; // yyx
55+
float mx_x2y0; // xx
56+
float mx_x0y2; // yy
57+
float mx_x1y1; // xy
58+
float mx_x1y0; // x
59+
float mx_x0y1; // y
60+
float mx_x0y0; // 1
61+
62+
float my_x3y0; // xxx
63+
float my_x0y3; // yyy
64+
float my_x2y1; // xxy
65+
float my_x1y2; // yyx
66+
float my_x2y0; // xx
67+
float my_x0y2; // yy
68+
float my_x1y1; // xy
69+
float my_x1y0; // x
70+
float my_x0y1; // y
71+
float my_x0y0; // 1
4872
};
4973

5074
struct IrCameraParams
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file
5+
* for details.
6+
*
7+
* This code is licensed to you under the terms of the Apache License, version
8+
* 2.0, or, at your option, the terms of the GNU General Public License,
9+
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10+
* or the following URLs:
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* http://www.gnu.org/licenses/gpl-2.0.txt
13+
*
14+
* If you redistribute this file in source form, modified or unmodified, you
15+
* may:
16+
* 1) Leave this header intact and distribute it under the same terms,
17+
* accompanying it with the APACHE20 and GPL20 files, or
18+
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19+
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20+
* In all cases you must keep the copyright notice intact and include a copy
21+
* of the CONTRIB file.
22+
*
23+
* Binary distributions must follow the binary distribution requirements of
24+
* either License.
25+
*/
26+
27+
#ifndef REGISTRATION_H_
28+
#define REGISTRATION_H_
29+
30+
#include <string>
31+
#include <libfreenect2/config.h>
32+
#include <libfreenect2/libfreenect2.hpp>
33+
34+
namespace libfreenect2
35+
{
36+
37+
class LIBFREENECT2_API Registration
38+
{
39+
public:
40+
Registration(Freenect2Device::IrCameraParams *depth_p, Freenect2Device::ColorCameraParams *rgb_p);
41+
42+
void apply( int dx, int dy, float dz, float& cx, float &cy);
43+
44+
private:
45+
void undistort_depth(int dx, int dy, float& mx, float& my);
46+
void depth_to_color(float mx, float my, float& rx, float& ry);
47+
48+
Freenect2Device::IrCameraParams depth;
49+
Freenect2Device::ColorCameraParams color;
50+
51+
float undistort_map[512][424][2];
52+
float depth_to_color_map[512][424][2];
53+
};
54+
55+
} /* namespace libfreenect2 */
56+
#endif /* REGISTRATION_H_ */

examples/protonect/src/libfreenect2.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,31 @@ void Freenect2DeviceImpl::start()
480480
rgb_camera_params_.cx = rgb_p->color_cx;
481481
rgb_camera_params_.cy = rgb_p->color_cy;
482482

483+
rgb_camera_params_.shift_d = rgb_p->shift_d;
484+
rgb_camera_params_.shift_m = rgb_p->shift_m;
485+
486+
rgb_camera_params_.mx_x3y0 = rgb_p->mx_x3y0; // xxx
487+
rgb_camera_params_.mx_x0y3 = rgb_p->mx_x0y3; // yyy
488+
rgb_camera_params_.mx_x2y1 = rgb_p->mx_x2y1; // xxy
489+
rgb_camera_params_.mx_x1y2 = rgb_p->mx_x1y2; // yyx
490+
rgb_camera_params_.mx_x2y0 = rgb_p->mx_x2y0; // xx
491+
rgb_camera_params_.mx_x0y2 = rgb_p->mx_x0y2; // yy
492+
rgb_camera_params_.mx_x1y1 = rgb_p->mx_x1y1; // xy
493+
rgb_camera_params_.mx_x1y0 = rgb_p->mx_x1y0; // x
494+
rgb_camera_params_.mx_x0y1 = rgb_p->mx_x0y1; // y
495+
rgb_camera_params_.mx_x0y0 = rgb_p->mx_x0y0; // 1
496+
497+
rgb_camera_params_.my_x3y0 = rgb_p->my_x3y0; // xxx
498+
rgb_camera_params_.my_x0y3 = rgb_p->my_x0y3; // yyy
499+
rgb_camera_params_.my_x2y1 = rgb_p->my_x2y1; // xxy
500+
rgb_camera_params_.my_x1y2 = rgb_p->my_x1y2; // yyx
501+
rgb_camera_params_.my_x2y0 = rgb_p->my_x2y0; // xx
502+
rgb_camera_params_.my_x0y2 = rgb_p->my_x0y2; // yy
503+
rgb_camera_params_.my_x1y1 = rgb_p->my_x1y1; // xy
504+
rgb_camera_params_.my_x1y0 = rgb_p->my_x1y0; // x
505+
rgb_camera_params_.my_x0y1 = rgb_p->my_x0y1; // y
506+
rgb_camera_params_.my_x0y0 = rgb_p->my_x0y0; // 1
507+
483508
command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result);
484509
std::cout << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl;
485510
std::cout << GenericResponse(result.data, result.length).toString() << std::endl;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file
5+
* for details.
6+
*
7+
* This code is licensed to you under the terms of the Apache License, version
8+
* 2.0, or, at your option, the terms of the GNU General Public License,
9+
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10+
* or the following URLs:
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* http://www.gnu.org/licenses/gpl-2.0.txt
13+
*
14+
* If you redistribute this file in source form, modified or unmodified, you
15+
* may:
16+
* 1) Leave this header intact and distribute it under the same terms,
17+
* accompanying it with the APACHE20 and GPL20 files, or
18+
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19+
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20+
* In all cases you must keep the copyright notice intact and include a copy
21+
* of the CONTRIB file.
22+
*
23+
* Binary distributions must follow the binary distribution requirements of
24+
* either License.
25+
*/
26+
27+
#include <math.h>
28+
#include <libfreenect2/registration.h>
29+
30+
namespace libfreenect2
31+
{
32+
33+
/*
34+
* most information, including the table layout in command_response.h, was
35+
* provided by @sh0 in https://github.com/OpenKinect/libfreenect2/issues/41
36+
*/
37+
38+
// these seem to be hardcoded in the original SDK
39+
static const float depth_q = 0.01;
40+
static const float color_q = 0.002199;
41+
42+
void Registration::undistort_depth(int x, int y, float& mx, float& my)
43+
{
44+
float dx = ((float)x - depth.cx) / depth.fx;
45+
float dy = ((float)y - depth.cy) / depth.fy;
46+
47+
float ps = (dx * dx) + (dy * dy);
48+
float qs = ((ps * depth.k3 + depth.k2) * ps + depth.k1) * ps + 1.0;
49+
for (int i = 0; i < 9; i++) {
50+
float qd = ps / (qs * qs);
51+
qs = ((qd * depth.k3 + depth.k2) * qd + depth.k1) * qd + 1.0;
52+
}
53+
54+
mx = dx / qs;
55+
my = dy / qs;
56+
}
57+
58+
void Registration::depth_to_color(float mx, float my, float& rx, float& ry)
59+
{
60+
mx *= depth.fx * depth_q;
61+
my *= depth.fy * depth_q;
62+
63+
float wx =
64+
(mx * mx * mx * color.mx_x3y0) + (my * my * my * color.mx_x0y3) +
65+
(mx * mx * my * color.mx_x2y1) + (my * my * mx * color.mx_x1y2) +
66+
(mx * mx * color.mx_x2y0) + (my * my * color.mx_x0y2) + (mx * my * color.mx_x1y1) +
67+
(mx * color.mx_x1y0) + (my * color.mx_x0y1) + (color.mx_x0y0);
68+
69+
float wy =
70+
(mx * mx * mx * color.my_x3y0) + (my * my * my * color.my_x0y3) +
71+
(mx * mx * my * color.my_x2y1) + (my * my * mx * color.my_x1y2) +
72+
(mx * mx * color.my_x2y0) + (my * my * color.my_x0y2) + (mx * my * color.my_x1y1) +
73+
(mx * color.my_x1y0) + (my * color.my_x0y1) + (color.my_x0y0);
74+
75+
rx = wx / (color.fx * color_q);
76+
ry = wy / (color.fx * color_q);
77+
}
78+
79+
void Registration::apply( int dx, int dy, float dz, float& cx, float &cy)
80+
{
81+
float rx = depth_to_color_map[dx][dy][0];
82+
float ry = depth_to_color_map[dx][dy][1];
83+
84+
rx += (color.shift_m / dz) - (color.shift_m / color.shift_d);
85+
86+
cx = rx * color.fx + color.cx;
87+
cy = ry * color.fy + color.cy;
88+
}
89+
90+
Registration::Registration(Freenect2Device::IrCameraParams *depth_p, Freenect2Device::ColorCameraParams *rgb_p):
91+
depth(*depth_p), color(*rgb_p)
92+
{
93+
float mx, my;
94+
float rx, ry;
95+
96+
for (int x = 0; x < 512; x++)
97+
for (int y = 0; y < 424; y++) {
98+
undistort_depth(x,y,mx,my);
99+
undistort_map[x][y][0] = mx;
100+
undistort_map[x][y][1] = my;
101+
}
102+
103+
for (int x = 0; x < 512; x++)
104+
for (int y = 0; y < 424; y++) {
105+
undistort_depth(x,y,mx,my);
106+
depth_to_color(mx,my,rx,ry);
107+
depth_to_color_map[x][y][0] = rx;
108+
depth_to_color_map[x][y][1] = ry;
109+
}
110+
}
111+
112+
} /* namespace libfreenect2 */

0 commit comments

Comments
 (0)