Skip to content

Commit 9069ddf

Browse files
committed
Adds zbuffer to the mapping of rgb to depth
Signed-off-by: Francisco Facioni <[email protected]>
1 parent be35d02 commit 9069ddf

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

src/registration.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,36 +334,66 @@ void freenect_map_rgb_to_depth(freenect_device* dev, uint16_t* depth_mm, uint8_t
334334
{
335335
uint32_t target_offset = dev->registration.reg_pad_info.start_lines * DEPTH_Y_RES;
336336
int x,y;
337+
int* map = (int*)malloc(DEPTH_Y_RES*DEPTH_X_RES* sizeof(int));
338+
unsigned short* zBuffer = (unsigned short*)malloc(DEPTH_Y_RES*DEPTH_X_RES* sizeof(unsigned short));
339+
memset(zBuffer, DEPTH_NO_MM_VALUE, DEPTH_X_RES*DEPTH_Y_RES * sizeof(unsigned short));
337340

338341
for (y = 0; y < DEPTH_Y_RES; y++) for (x = 0; x < DEPTH_X_RES; x++) {
339-
340342
uint32_t index = y * DEPTH_X_RES + x;
341343
uint32_t cx,cy,cindex;
342344

345+
map[index] = -1;
346+
343347
int wz = depth_mm[index];
344348

345-
// pixels without depth data are black
346-
if (wz == 0) {
347-
index = index*3;
348-
rgb_registered[index+0] = 0;
349-
rgb_registered[index+1] = 0;
350-
rgb_registered[index+2] = 0;
349+
if (wz == DEPTH_NO_MM_VALUE) {
351350
continue;
352351
}
353352

354353
// coordinates in rgb image corresponding to x,y in depth image
355-
cx = (dev->registration.registration_table[index][0] + dev->registration.depth_to_rgb_shift[wz]) / REG_X_VAL_SCALE;
356-
cy = dev->registration.registration_table[index][1];
354+
cx = (dev->registration.registration_table[index][0] + dev->registration.depth_to_rgb_shift[wz]) / REG_X_VAL_SCALE;
355+
cy = dev->registration.registration_table[index][1] - target_offset;
357356

358357
if (cx >= DEPTH_X_RES) continue;
359358

360-
cindex = (cy * DEPTH_X_RES + cx - target_offset) * 3;
361-
index = index*3;
359+
cindex = cy*DEPTH_X_RES+cx;
360+
map[index] = cindex;
361+
362+
if (zBuffer[cindex] == DEPTH_NO_MM_VALUE || zBuffer[cindex] > wz) {
363+
zBuffer[cindex] = wz;
364+
}
365+
}
366+
367+
for (y = 0; y < DEPTH_Y_RES; y++) for (x = 0; x < DEPTH_X_RES; x++) {
368+
uint32_t index = y * DEPTH_X_RES + x;
369+
uint32_t cindex = map[index];
370+
371+
// pixels without depth data or out of bounds are black
372+
if (cindex == -1) {
373+
index *= 3;
374+
rgb_registered[index+0] = 0;
375+
rgb_registered[index+1] = 0;
376+
rgb_registered[index+2] = 0;
362377

363-
rgb_registered[index+0] = rgb_raw[cindex+0];
364-
rgb_registered[index+1] = rgb_raw[cindex+1];
365-
rgb_registered[index+2] = rgb_raw[cindex+2];
378+
continue;
379+
}
380+
381+
unsigned short currentDepth = depth_mm[index];
382+
unsigned short minDepth = zBuffer[cindex];
383+
384+
// filters out pixels that are occluded
385+
if (currentDepth <= minDepth) {
386+
index *= 3;
387+
cindex *= 3;
388+
389+
rgb_registered[index+0] = rgb_raw[cindex+0];
390+
rgb_registered[index+1] = rgb_raw[cindex+1];
391+
rgb_registered[index+2] = rgb_raw[cindex+2];
392+
}
366393
}
394+
395+
free(zBuffer);
396+
free(map);
367397
}
368398

369399
/// Allocate and fill registration tables

0 commit comments

Comments
 (0)