# Limited Area Depth data

• ### Question

• I had a area limited by RightHand position (X,Y) and +- reference distance, for X and Y is the same calcule.

I just want to obtain distance in that area.

The depth data is obtained How??

I tryed:

```short[] rawDepthData = new short[depth.PixelDataLength];
depth.CopyPixelDataTo(rawDepthData);

for (int depthIndexX = (int)Math.Round(RightHandPX) - RefDistance; depthIndexX < (int)Math.Round(RightHandPX) + RefDistance; depthIndexX++)
{
for (int depthIndexY = (int)Math.Round(RightHandPY) - RefDistance; depthIndexY < (int)Math.Round(RightHandPY) + RefDistance; depthIndexY++)
{

int dist = rawDepthData[(depthIndexY + depthIndexX)] >> DepthImageFrame.PlayerIndexBitmaskWidth;

...```
RightHandPX is the position of right hand in pixel. RightHandPY is the same but for Y.
Friday, May 4, 2012 4:14 PM

• I think the formula you want is:

int dist = rawDepthData[(depthIndexY * depth.Width) + depthIndexX] >> DepthImageFrame.PlayerIndexBitmaskWidth;

John
K4W Dev

Friday, May 4, 2012 5:41 PM

### All replies

• I think the formula you want is:

int dist = rawDepthData[(depthIndexY * depth.Width) + depthIndexX] >> DepthImageFrame.PlayerIndexBitmaskWidth;

John
K4W Dev

Friday, May 4, 2012 5:41 PM
• since you are dealing with an area of pixels, you need to average the range of all the pixels. Keep in mind to make sure your ranges don't go past the bounds of the array.

int totalDistance = 0;

int counter = 0;

for(int y = refY - 5; y < refY + 5; y++)

if( y < 0 || y > DepthImageHeight )
continue;

for(int x = refX - 5; x < refX + 5; x++)

if (x < 0 || x > DepthImageWidth )
continue;

(if we are here the x,y is valid)
totalDistance += rawDepthData[(y * (Width of Depthframe) + x)] >> DepthImageFrame.PlayerIndexBitmaskWidth;
counter++;

}

}

// calculate the average of the distances

double avgDist = totalDistance / counter;

Friday, May 4, 2012 5:49 PM
• Thanks was it that i searching for.