|
| 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