@@ -41,6 +41,7 @@ static const float color_q = 0.002199;
4141
4242void Registration::distort (int mx, int my, float & x, float & y) const
4343{
44+ // see http://en.wikipedia.org/wiki/Distortion_(optics) for description
4445 float dx = ((float )mx - depth.cx ) / depth.fx ;
4546 float dy = ((float )my - depth.cy ) / depth.fy ;
4647 float dx2 = dx * dx;
@@ -85,6 +86,7 @@ void Registration::apply( int dx, int dy, float dz, float& cx, float &cy) const
8586
8687void Registration::apply (const Frame *rgb, const Frame *depth, Frame *undistorted, Frame *registered) const
8788{
89+ // Check if all frames are valid and have the correct size
8890 if (!undistorted || !rgb || !registered ||
8991 rgb->width != 1920 || rgb->height != 1080 || rgb->bytes_per_pixel != 3 ||
9092 depth->width != 512 || depth->height != 424 || depth->bytes_per_pixel != 4 ||
@@ -100,10 +102,15 @@ void Registration::apply(const Frame *rgb, const Frame *depth, Frame *undistorte
100102 const int *map_yi = depth_to_color_map_yi;
101103 const int size_depth = 512 * 424 ;
102104 const int size_color = 1920 * 1080 * 3 ;
105+ const float color_cx = color.cx + 0 .5f ; // 0.5f added for later rounding
103106
107+ // iterating over all pixels from undistorted depth and registered color image
108+ // the three maps have the same structure as the images, so their pointers are increased each iteration as well
104109 for (int i = 0 ; i < size_depth; ++i, ++registered_data, ++undistorted_data, ++map_dist, ++map_x, ++map_yi) {
110+ // getting index of distorted depth pixel
105111 const int index = *map_dist;
106112
113+ // check if distorted depth pixel is outside of the depth image
107114 if (index < 0 ){
108115 *undistorted_data = 0 ;
109116 *registered_data = 0 ;
@@ -112,28 +119,36 @@ void Registration::apply(const Frame *rgb, const Frame *depth, Frame *undistorte
112119 continue ;
113120 }
114121
122+ // getting depth value for current pixel
115123 const float z_raw = depth_data[index];
116124 *undistorted_data = z_raw;
117125
126+ // checking for invalid depth value
118127 if (z_raw <= 0 .0f ) {
119128 *registered_data = 0 ;
120129 *++registered_data = 0 ;
121130 *++registered_data = 0 ;
122131 continue ;
123132 }
124133
125- const float rx = (*map_x + (color.shift_m / z_raw)) * color.fx + color.cx ;
126- const int cx = rx + 0 .5f ; // same as round for positive numbers
134+ // calculating x offset for rgb image based on depth value
135+ const float rx = (*map_x + (color.shift_m / z_raw)) * color.fx + color_cx;
136+ const int cx = rx; // same as round for positive numbers (0.5f was already added to color_cx)
137+ // getting y offset for depth image
127138 const int cy = *map_yi;
139+ // combining offsets
128140 const int c_off = cx * 3 + cy;
129141
130- if (c_off < 0 || c_off > size_color || rx < -0 .5f ) {
142+ // check if c_off is outside of rgb image
143+ // checking rx/cx is not needed because the color image is much wider then the depth image
144+ if (c_off < 0 || c_off >= size_color) {
131145 *registered_data = 0 ;
132146 *++registered_data = 0 ;
133147 *++registered_data = 0 ;
134148 continue ;
135149 }
136150
151+ // Setting RGB or registered image
137152 const unsigned char *rgb_data = rgb->data + c_off;
138153 *registered_data = *rgb_data;
139154 *++registered_data = *++rgb_data;
@@ -154,18 +169,23 @@ Registration::Registration(Freenect2Device::IrCameraParams depth_p, Freenect2Dev
154169
155170 for (int y = 0 ; y < 424 ; y++) {
156171 for (int x = 0 ; x < 512 ; x++) {
172+ // compute the dirstored coordinate for current pixel
157173 distort (x,y,mx,my);
174+ // rounding the values and check if the pixel is inside the image
158175 ix = roundf (mx);
159176 iy = roundf (my);
160177 if (ix < 0 || ix >= 512 || iy < 0 || iy >= 424 )
161178 index = -1 ;
162179 else
180+ // computing the index from the coordianted for faster access to the data
163181 index = iy * 512 + ix;
164182 *map_dist++ = index;
165183
184+ // compute the depth to color mapping entries for the current pixel
166185 depth_to_color (x,y,rx,ry);
167186 *map_x++ = rx;
168187 *map_y++ = ry;
188+ // compute the y offset to minimize later computations
169189 *map_yi++ = roundf (ry) * 1920 * 3 ;
170190 }
171191 }
0 commit comments