@@ -72,78 +72,86 @@ void Registration::depth_to_color(float mx, float my, float& rx, float& ry)
7272 (mx * mx * color.my_x2y0 ) + (my * my * color.my_x0y2 ) + (mx * my * color.my_x1y1 ) +
7373 (mx * color.my_x1y0 ) + (my * color.my_x0y1 ) + (color.my_x0y0 );
7474
75- rx = wx / (color.fx * color_q);
76- ry = wy / (color.fx * color_q);
75+ rx = ( wx / (color.fx * color_q)) - (color. shift_m / color. shift_d );
76+ ry = ( wy / (color.fx * color_q)) * color. fy + color. cy ;
7777}
7878
79- void Registration::apply ( int dx, int dy, float dz, float & cx, float &cy)
79+ void Registration::apply ( int dx, int dy, float dz, float & cx, float &cy) const
8080{
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 );
81+ const int index = dx + dy * 512 ;
82+ float rx = depth_to_color_map_x[index];
83+ cy = depth_to_color_map_y[index];
8584
85+ rx += (color.shift_m / dz);
8686 cx = rx * color.fx + color.cx ;
87- cy = ry * color.fy + color.cy ;
8887}
8988
90- void Registration::apply (Frame* rgb, Frame* depth, unsigned char * registered)
89+ void Registration::apply (const Frame* rgb, const Frame* depth, Frame * registered) const
9190{
92- if (!depth || !rgb || !registered)
91+ if (!depth || !rgb || !registered ||
92+ depth->width != 512 || depth->height != 424 || depth->bytes_per_pixel != 4 ||
93+ rgb->width != 1920 || rgb->height != 1080 || rgb->bytes_per_pixel != 3 ||
94+ registered->width != 512 || registered->height != 424 || registered->bytes_per_pixel != 3 )
9395 return ;
9496
95- float * depth_raw = (float *)depth->data ;
96- float cx, cy;
97- int c_off, d_off, r_off;
98-
99- for (int x = 0 ; x < depth->width ; x++) {
100- for (int y = 0 ; y < depth->height ; y++) {
101-
102- d_off = y*depth->width + x;
103- r_off = d_off*rgb->bytes_per_pixel ;
104-
105- float z_raw = depth_raw[d_off];
106- if (z_raw == 0.0 ) {
107- registered[r_off+0 ] = 0 ;
108- registered[r_off+1 ] = 0 ;
109- registered[r_off+2 ] = 0 ;
110- continue ;
111- }
112-
113- apply (x,y,z_raw,cx,cy);
114-
115- c_off = (round (cx) + round (cy) * rgb->width ) * rgb->bytes_per_pixel ;
116- if ((c_off < 0 ) || (c_off > rgb->width *rgb->height *rgb->bytes_per_pixel )) {
117- registered[r_off+0 ] = 0 ;
118- registered[r_off+1 ] = 0 ;
119- registered[r_off+2 ] = 0 ;
120- continue ;
121- }
122-
123- registered[r_off+0 ] = rgb->data [c_off+0 ];
124- registered[r_off+1 ] = rgb->data [c_off+1 ];
125- registered[r_off+2 ] = rgb->data [c_off+2 ];
97+ const float *depth_raw = (float *)depth->data ;
98+ const float *map_x = depth_to_color_map_x;
99+ const int *map_i = depth_to_color_map_i;
100+ unsigned char *registered_raw = registered->data ;
101+ const int size_depth = 512 * 424 ;
102+ const int size_color = 1920 * 1080 * 3 ;
103+
104+ for (int i = 0 ; i < size_depth; ++i, ++registered_raw, ++map_x, ++map_i, ++depth_raw) {
105+ const float z_raw = *depth_raw;
106+
107+ if (z_raw == 0.0 ) {
108+ *registered_raw = 0 ;
109+ *++registered_raw = 0 ;
110+ *++registered_raw = 0 ;
111+ continue ;
126112 }
127- }
128113
114+ const float rx = (*map_x + (color.shift_m / z_raw)) * color.fx + color.cx ;
115+ const int cx = rx + 0 .5f ; // same as round for positive numbers
116+ const int cy = *map_i;
117+ const int c_off = cx * 3 + cy;
118+
119+ if (c_off < 0 || c_off > size_color || rx < -0 .5f ) {
120+ *registered_raw = 0 ;
121+ *++registered_raw = 0 ;
122+ *++registered_raw = 0 ;
123+ continue ;
124+ }
125+
126+ const unsigned char *rgb_data = rgb->data + c_off;
127+ *registered_raw = *rgb_data;
128+ *++registered_raw = *++rgb_data;
129+ *++registered_raw = *++rgb_data;
130+ }
129131}
130132
131133Registration::Registration (Freenect2Device::IrCameraParams depth_p, Freenect2Device::ColorCameraParams rgb_p):
132134 depth (depth_p), color(rgb_p)
133135{
134136 float mx, my;
135137 float rx, ry;
138+ float *it_undist = undistort_map;
139+ float *map_x = depth_to_color_map_x;
140+ float *map_y = depth_to_color_map_y;
141+ int *map_i = depth_to_color_map_i;
136142
137- for (int x = 0 ; x < 512 ; x ++)
138- for (int y = 0 ; y < 424 ; y ++) {
143+ for (int y = 0 ; y < 424 ; y ++) {
144+ for (int x = 0 ; x < 512 ; x ++) {
139145
140146 undistort_depth (x,y,mx,my);
141- undistort_map[x][y][ 0 ] = mx;
142- undistort_map[x][y][ 1 ] = my;
147+ *it_undist++ = mx;
148+ *it_undist++ = my;
143149
144150 depth_to_color (mx,my,rx,ry);
145- depth_to_color_map[x][y][0 ] = rx;
146- depth_to_color_map[x][y][1 ] = ry;
151+ *map_x++ = rx;
152+ *map_y++ = ry;
153+ *map_i++ = round (ry) * 1920 * 3 ;
154+ }
147155 }
148156}
149157
0 commit comments