@@ -50,7 +50,9 @@ class RegistrationImpl
5050
5151 void apply (int dx, int dy, float dz, float & cx, float &cy) const ;
5252 void apply (const Frame* rgb, const Frame* depth, Frame* undistorted, Frame* registered, const bool enable_filter, Frame* bigdepth, int * color_depth_map) const ;
53+ void undistortDepth (const Frame *depth, Frame *undistorted) const ;
5354 void getPointXYZRGB (const Frame* undistorted, const Frame* registered, int r, int c, float & x, float & y, float & z, float & rgb) const ;
55+ void getPointXYZ (const Frame* undistorted, int r, int c, float & x, float & y, float & z) const ;
5456 void distort (int mx, int my, float & dx, float & dy) const ;
5557 void depth_to_color (float mx, float my, float & rx, float & ry) const ;
5658
@@ -271,33 +273,89 @@ void RegistrationImpl::apply(const Frame *rgb, const Frame *depth, Frame *undist
271273 if (!color_depth_map) delete[] depth_to_c_off;
272274}
273275
276+ void Registration::undistortDepth (const Frame *depth, Frame *undistorted) const
277+ {
278+ impl_->undistortDepth (depth, undistorted);
279+ }
280+
281+ void RegistrationImpl::undistortDepth (const Frame *depth, Frame *undistorted) const
282+ {
283+ // Check if all frames are valid and have the correct size
284+ if (!depth || !undistorted ||
285+ depth->width != 512 || depth->height != 424 || depth->bytes_per_pixel != 4 ||
286+ undistorted->width != 512 || undistorted->height != 424 || undistorted->bytes_per_pixel != 4 )
287+ return ;
288+
289+ const float *depth_data = (float *)depth->data ;
290+ float *undistorted_data = (float *)undistorted->data ;
291+ const int *map_dist = distort_map;
292+
293+ const int size_depth = 512 * 424 ;
294+
295+ /* Fix depth distortion, and compute pixel to use from 'rgb' based on depth measurement,
296+ * stored as x/y offset in the rgb data.
297+ */
298+
299+ // iterating over all pixels from undistorted depth and registered color image
300+ // the four maps have the same structure as the images, so their pointers are increased each iteration as well
301+ for (int i = 0 ; i < size_depth; ++i, ++undistorted_data, ++map_dist){
302+ // getting index of distorted depth pixel
303+ const int index = *map_dist;
304+
305+ // check if distorted depth pixel is outside of the depth image
306+ if (index < 0 ){
307+ *undistorted_data = 0 ;
308+ continue ;
309+ }
310+
311+ // getting depth value for current pixel
312+ const float z = depth_data[index];
313+ *undistorted_data = z;
314+ }
315+ }
316+
274317void Registration::getPointXYZRGB (const Frame* undistorted, const Frame* registered, int r, int c, float & x, float & y, float & z, float & rgb) const
275318{
276319 impl_->getPointXYZRGB (undistorted, registered, r, c, x, y, z, rgb);
277320}
278321
279322void RegistrationImpl::getPointXYZRGB (const Frame* undistorted, const Frame* registered, int r, int c, float & x, float & y, float & z, float & rgb) const
323+ {
324+ getPointXYZ (undistorted, r, c, x, y, z);
325+
326+ if (isnan (z))
327+ {
328+ rgb = 0 ;
329+ }
330+ else
331+ {
332+ float * registered_data = (float *)registered->data ;
333+ rgb = registered_data[512 *r+c];
334+ }
335+ }
336+
337+ void Registration::getPointXYZ (const Frame *undistorted, int r, int c, float &x, float &y, float &z) const
338+ {
339+ impl_->getPointXYZ (undistorted,r,c,x,y,z);
340+ }
341+
342+ void RegistrationImpl::getPointXYZ (const Frame *undistorted, int r, int c, float &x, float &y, float &z) const
280343{
281344 const float bad_point = std::numeric_limits<float >::quiet_NaN ();
282345 const float cx (depth.cx ), cy (depth.cy );
283346 const float fx (1 /depth.fx ), fy (1 /depth.fy );
284347 float * undistorted_data = (float *)undistorted->data ;
285- float * registered_data = (float *)registered->data ;
286348 const float depth_val = undistorted_data[512 *r+c]/1000 .0f ; // scaling factor, so that value of 1 is one meter.
287349 if (isnan (depth_val) || depth_val <= 0.001 )
288350 {
289351 // depth value is not valid
290352 x = y = z = bad_point;
291- rgb = 0 ;
292- return ;
293353 }
294354 else
295355 {
296356 x = (c + 0.5 - cx) * fx * depth_val;
297357 y = (r + 0.5 - cy) * fy * depth_val;
298358 z = depth_val;
299- rgb = *reinterpret_cast <float *>(®istered_data[512 *r+c]);
300- return ;
301359 }
302360}
303361
0 commit comments