|
| 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(float dx, float dy, float& mx, float& my) |
| 43 | +{ |
| 44 | + float ps = (dx * dx) + (dy * dy); |
| 45 | + float qs = ((ps * depth_k3 + depth_k2) * ps + depth_k1) * ps + 1.0; |
| 46 | + for (int i = 0; i < 9; i++) { |
| 47 | + float qd = ps / (qs * qs); |
| 48 | + qs = ((qd * depth_k3 + depth_k2) * qd + depth_k1) * qd + 1.0; |
| 49 | + } |
| 50 | + mx = dx / qs; |
| 51 | + my = dy / qs; |
| 52 | +} |
| 53 | + |
| 54 | +/*void kinect2_depth_to_color(float mx, float my, float z, float& rx, float& ry) |
| 55 | +{ |
| 56 | + mx *= depth_f * depth_q; |
| 57 | + my *= depth_f * depth_q; |
| 58 | +
|
| 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); |
| 69 | +
|
| 70 | + rx = wx / (color_f * color_q); |
| 71 | + ry = wy / (color_f * color_q); |
| 72 | +
|
| 73 | + rx += (shift_m / z) - (shift_m / shift_d); |
| 74 | +
|
| 75 | + rx = rx * color_f + color_cx; |
| 76 | + ry = ry * color_f + color_cy; |
| 77 | +}*/ |
| 78 | + |
| 79 | +Registration::Registration(protocol::DepthCameraParamsResponse *depth_p, protocol::RgbCameraParamsResponse *rgb_p) |
| 80 | +{ |
| 81 | + float mx, my; |
| 82 | + int rx, ry; |
| 83 | + |
| 84 | + depth_k1 = depth_p->k1; |
| 85 | + depth_k2 = depth_p->k2; |
| 86 | + depth_k3 = depth_p->k3; |
| 87 | + |
| 88 | + for (int x = 0; x < 512; x++) |
| 89 | + for (int y = 0; y < 424; y++) { |
| 90 | + undistort_depth(x,y,mx,my); |
| 91 | + rx = round(mx); |
| 92 | + ry = round(my); |
| 93 | + undistort_map[rx][ry][0] = x; |
| 94 | + undistort_map[rx][ry][1] = y; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +} /* namespace libfreenect2 */ |
0 commit comments