Skip to content

Commit 9f19d7a

Browse files
committed
Merge pull request #549 from matthieu-ft/master
registration: Add depth-only methods
2 parents 548391e + 9f39b74 commit 9f19d7a

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

include/libfreenect2/registration.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class LIBFREENECT2_API Registration
8181
*/
8282
void apply(const Frame* rgb, const Frame* depth, Frame* undistorted, Frame* registered, const bool enable_filter = true, Frame* bigdepth = 0, int* color_depth_map = 0) const;
8383

84+
/** Undistort depth
85+
* @param depth Depth image (512x424 float)
86+
* @param[out] undistorted Undistorted depth image
87+
*/
88+
void undistortDepth(const Frame* depth, Frame* undistorted) const;
89+
8490
/** Construct a 3-D point with color in a point cloud.
8591
* @param undistorted Undistorted depth frame from apply().
8692
* @param registered Registered color frame from apply().
@@ -98,6 +104,16 @@ class LIBFREENECT2_API Registration
98104
*/
99105
void getPointXYZRGB (const Frame* undistorted, const Frame* registered, int r, int c, float& x, float& y, float& z, float& rgb) const;
100106

107+
/** Construct a 3-D point in a point cloud.
108+
* @param undistorted Undistorted depth frame from apply().
109+
* @param r Row (y) index in depth image.
110+
* @param c Column (x) index in depth image.
111+
* @param[out] x X coordinate of the 3-D point (meter).
112+
* @param[out] y Y coordinate of the 3-D point (meter).
113+
* @param[out] z Z coordinate of the 3-D point (meter).
114+
*/
115+
void getPointXYZ (const Frame* undistorted, int r, int c, float& x, float& y, float& z) const;
116+
101117
private:
102118
RegistrationImpl *impl_;
103119
};

src/registration.cpp

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
274317
void 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

279322
void 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*>(&registered_data[512*r+c]);
300-
return;
301359
}
302360
}
303361

0 commit comments

Comments
 (0)