Skip to content

Commit 4149685

Browse files
author
Thiemo Wiedemeyer
committed
made filtering optional, but enabled by default.
1 parent 05d73e0 commit 4149685

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

examples/protonect/include/libfreenect2/registration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class LIBFREENECT2_API Registration
4444
void apply(int dx, int dy, float dz, float& cx, float &cy) const;
4545

4646
// undistort/register a whole image
47-
void apply(const Frame* rgb, const Frame* depth, Frame* undistorted, Frame* registered) const;
47+
void apply(const Frame* rgb, const Frame* depth, Frame* undistorted, Frame* registered, const bool enable_filter = true) const;
4848

4949
private:
5050
void distort(int mx, int my, float& dx, float& dy) const;

examples/protonect/src/registration.cpp

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void Registration::apply( int dx, int dy, float dz, float& cx, float &cy) const
8484
cx = rx * color.fx + color.cx;
8585
}
8686

87-
void Registration::apply(const Frame *rgb, const Frame *depth, Frame *undistorted, Frame *registered) const
87+
void Registration::apply(const Frame *rgb, const Frame *depth, Frame *undistorted, Frame *registered, const bool enable_filter) const
8888
{
8989
// Check if all frames are valid and have the correct size
9090
if (!undistorted || !rgb || !registered ||
@@ -112,17 +112,22 @@ void Registration::apply(const Frame *rgb, const Frame *depth, Frame *undistorte
112112
const int offset_filter_map = 1920 * filter_height_half;
113113

114114
// map for storing the min z values used for each color pixel
115-
float *filter_map = new float[size_filter_map];
115+
float *filter_map = NULL;
116116
// pointer to the beginning of the important data
117-
float *p_filter_map = filter_map + offset_filter_map;
117+
float *p_filter_map = NULL;
118118

119119
// map for storing the color offest for each depth pixel
120120
int *depth_to_c_off = new int[size_depth];
121121
int *map_c_off = depth_to_c_off;
122122

123123
// initializing the depth_map with values outside of the Kinect2 range
124-
for(float *it = filter_map, *end = filter_map + size_filter_map; it != end; ++it){
125-
*it = 65536.0f;
124+
if(enable_filter){
125+
filter_map = new float[size_filter_map];
126+
p_filter_map = filter_map + offset_filter_map;
127+
128+
for(float *it = filter_map, *end = filter_map + size_filter_map; it != end; ++it){
129+
*it = 65536.0f;
130+
}
126131
}
127132

128133
// iterating over all pixels from undistorted depth and registered color image
@@ -163,53 +168,59 @@ void Registration::apply(const Frame *rgb, const Frame *depth, Frame *undistorte
163168
continue;
164169
}
165170

166-
// setting a window around the filter map pixel corresponding to the color pixel with the current z value
167-
int yi = (cy - filter_height_half) * 1920 + cx - filter_width_half; // index of first pixel to set
168-
for(int r = -filter_height_half; r <= filter_height_half; ++r, yi += 1920) // index increased by a full row each iteration
169-
{
170-
float *it = p_filter_map + yi;
171-
for(int c = -filter_width_half; c <= filter_width_half; ++c, ++it)
171+
// saving the offset for later
172+
*map_c_off = c_off;
173+
174+
if(enable_filter){
175+
// setting a window around the filter map pixel corresponding to the color pixel with the current z value
176+
int yi = (cy - filter_height_half) * 1920 + cx - filter_width_half; // index of first pixel to set
177+
for(int r = -filter_height_half; r <= filter_height_half; ++r, yi += 1920) // index increased by a full row each iteration
172178
{
173-
// only set if the current z is smaller
174-
if(z < *it)
175-
*it = z;
179+
float *it = p_filter_map + yi;
180+
for(int c = -filter_width_half; c <= filter_width_half; ++c, ++it)
181+
{
182+
// only set if the current z is smaller
183+
if(z < *it)
184+
*it = z;
185+
}
176186
}
177187
}
178-
179-
// saving the offset for later
180-
*map_c_off = c_off;
181188
}
182189

183190
// reseting the pointers to the beginning
184-
undistorted_data = (float*)undistorted->data;
185191
map_c_off = depth_to_c_off;
192+
undistorted_data = (float*)undistorted->data;
186193

187-
// run through all registered color pixels and set them based on filter results
188-
for(int i = 0; i < size_depth; ++i, registered_data += registered->bytes_per_pixel, ++map_c_off, ++undistorted_data){
189-
const int c_off = *map_c_off;
194+
if(enable_filter){
195+
// run through all registered color pixels and set them based on filter results
196+
for(int i = 0; i < size_depth; ++i, ++map_c_off, ++undistorted_data, registered_data += registered->bytes_per_pixel){
197+
const int c_off = *map_c_off;
190198

191-
// check if offset is out of image
192-
if(c_off < 0){
193-
*(int*)registered_data = 0;
194-
continue;
195-
}
199+
// check if offset is out of image
200+
if(c_off < 0){
201+
*(int*)registered_data = 0;
202+
continue;
203+
}
196204

197-
const float min_z = p_filter_map[c_off];
198-
const float z = *undistorted_data;
205+
const float min_z = p_filter_map[c_off];
206+
const float z = *undistorted_data;
199207

200-
// check for allowed depth noise
201-
if((z - min_z) / z > filter_tolerance) {
202-
*(int*)registered_data = 0;
203-
continue;
208+
// check for allowed depth noise
209+
*(int*)registered_data = (z - min_z) / z > filter_tolerance ? 0 : *(int*)(rgb->data + c_off * rgb->bytes_per_pixel);
204210
}
205211

206-
// Setting RGB or registered image
207-
const int *rgb_data = (int*)(rgb->data + c_off * rgb->bytes_per_pixel);
208-
*(int*)registered_data = *rgb_data;
212+
// delete the temporary maps
213+
delete[] filter_map;
209214
}
215+
else
216+
{
217+
for(int i = 0; i < size_depth; ++i, ++map_c_off, registered_data += registered->bytes_per_pixel){
218+
const int c_off = *map_c_off;
210219

211-
// delete the temporary maps
212-
delete[] filter_map;
220+
// check if offset is out of image
221+
*(int*)registered_data = c_off < 0 ? 0 : *(int*)(rgb->data + c_off * rgb->bytes_per_pixel);
222+
}
223+
}
213224
delete[] depth_to_c_off;
214225
}
215226

0 commit comments

Comments
 (0)