# Depth map to depth in mm

• ### Question

• Hello,

I have a question regarding the depth map obtained from Kinect.

I have been able to read the depth and work with it and I have a question regarding conversion of depthmap to actual depth in mm. Below is some code that I have written to test what is coming out of the Kinect. Here, I have captured a frame of depth and used NUI_LOCKED_RECT to lock the frame. Then I proceed to write out the data from the depth map into a file. When I examined the file, it contained values from [0, 255] and not in physical distance (mm).

```          NUI_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);

printf("Size = %d\n", LockedRect.size);
data = fopen ("pixeldata.dat","w");
for ( int i = 0; i < 480*640; i++){
fprintf(data, "%hd\n", *(LockedRect.pBits + i));
}
fclose(data);```

So my question is :

1. Is there a reliable equation that converts the depth in grayscale back to depth in mm.
2. OR, am I missing something and can I get the depth in mm directly?

Thanks !

Ready to learn.. anytime anywhere anything :)

Thursday, July 26, 2012 10:06 PM

• I think NuiDepthPixelToDepth() function will be appropriated.

Check this out.

http://msdn.microsoft.com/en-us/library/nuiimagecamera.nuidepthpixeltodepth

In my case, I`m using this function to subtract the background. I`m not sure about Depth unit. but it will be helpful, I guess.

• Marked as answer by Monday, July 30, 2012 8:39 PM
Friday, July 27, 2012 3:24 AM
• Two things you'll want to do:

First, the data is 16 bits per pixel, so you'll need a pointer to USHORT:

const USHORT* pDepth = reinterpret_cast<const USHORT*>(LockedRect.pBits);

The depth in millimeters can be found in the high-order 13 bits of each USHORT. As Chan Seok Kang points out, the inline function NuiDepthPixelToDepth will extract the correct value for you:

fprintf(data, "%hd\n", NuiDepthPixelToDepth(*(pDepth + i)));

John
K4W Dev

• Marked as answer by Monday, July 30, 2012 8:39 PM
Monday, July 30, 2012 6:47 PM

### All replies

• I think NuiDepthPixelToDepth() function will be appropriated.

Check this out.

http://msdn.microsoft.com/en-us/library/nuiimagecamera.nuidepthpixeltodepth

In my case, I`m using this function to subtract the background. I`m not sure about Depth unit. but it will be helpful, I guess.

• Marked as answer by Monday, July 30, 2012 8:39 PM
Friday, July 27, 2012 3:24 AM
• Two things you'll want to do:

First, the data is 16 bits per pixel, so you'll need a pointer to USHORT:

const USHORT* pDepth = reinterpret_cast<const USHORT*>(LockedRect.pBits);

The depth in millimeters can be found in the high-order 13 bits of each USHORT. As Chan Seok Kang points out, the inline function NuiDepthPixelToDepth will extract the correct value for you:

fprintf(data, "%hd\n", NuiDepthPixelToDepth(*(pDepth + i)));

John
K4W Dev

• Marked as answer by Monday, July 30, 2012 8:39 PM
Monday, July 30, 2012 6:47 PM
• Thank you John and Chan -- that worked just perfectly !

Ready to learn.. anytime anywhere anything :)

Monday, July 30, 2012 8:39 PM