Today, I’m going to share a little trick that will save you a ton of time when working with Kinect: how to properly measure the distance between the sensor and a body or object. Surprisingly, I was asked the exact same question by three of my blog readers during the past 10 days!

First, some good-to-know background.

## How Kinect measures distances

As you know, Kinect integrates an infrared sensor, along with a depth processor. The visible area is called “field of view”. The depth processor produces depth frames. Each depth frame is a grid of 512×424 points. The Kinect SDK is then feeding the depth frames to a powerful body-detection algorithm. The algorithm identifies 25 human body joints and calculates their coordinates in the 3D space.

Every single joint has 3 values: X, Y, and Z. It is projected in a Cartesian coordinate system. The (0, 0, 0) point is the position of the sensor. Every other point is measured in terms of the position of the sensor! Check the overhead graphic below. It’s viewing the sensor and the scene from above.

- X is the position in the horizontal axis.
- Y is the position in the vertical axis.
- Z is the position in the depth axis.

## Making sense of the Z value

X and Y are fairly easy to understand. That’s not true for Z. If you take a close look at the graphic above, you’ll notice that the Z value is **not** the linear distance between the point and the sensor. Instead,** it’s the distance between the point and the plane of the sensor**! It’s like having a virtual wall right in front of the Kinect. The Z value is the vertical distance from that wall (drawn in green).

To access the Z value, use the code below:

`var distance = body.Joints[JointType.SpineBase].Position.Z;`

Simple, huh? The “body” variable represents the tracked player. The “Position” property is a set of X, Y, and Z coordinates in the 3D space. The “Z” value is the distance between the player and the plane. I’ve used the SpineBase joint as a reference, since it’s the most reliably-tracked joint, and it’s also closer to the center of mass. You can read more about processing body data in my body-tracking tutorial.

## Measuring the distance

How about the physical distance, though? The distance between the player and the device is represented by a mathematical vector (drawn in blue). Thankfully, we do not need to be gurus in Algebra to measure the length of a vector in the 3D space.

The length of a vector is given by the formula below:

The SDK provides us with all of the required values. All we have to do is convert the formula into working C# code:

```
public double Length(CameraSpacePoint point)
{
return Math.Sqrt(
point.X * point.X +
point.Y * point.Y +
point.Z * point.Z
);
}
```

Bringing it all together:

```
var point = body.Joints[JointType.SpineBase].Position;
var distance = Length(point);
```

This is it! You now know exactly how far or how close someone is to the Kinect sensor! Of course, if someone is standing right in front of the sensor, the Z value and the distance would converge.

*Hint: in case you need to measure distances of points that do not belong to the human body, you can use Coordinate Mapping.*

## Summary

Kinect provides us with the X, Y, and Z coordinates of the human body joints.

- To measure the distance between the player and the plane of the sensor, we just use the Z value as-is.
- To measure the distance between the player and the sensor, we calculate the length of the corresponding vector.

## PS: Vitruvius

If you liked this post, then you’ll love Vitruvius. Vitruvius is the result of my Kinect research during the past 4 years. Vitruvius will help you minimize the development time and create tough applications with just a few lines of code! Includes advanced Mathematics, Avateering, Video Recording, Face Tracking, and more.

‘Til the next time, keep Kinecting!

Unless Math.Pow gets inlined by the compiler (guess it doesn’t), better use x*x + y*y + z*z

Also if one can work with distance2 (distance squared), say if they just want to compare distances, instead of distance itself, they can avoid the cost of the square root calculation.

Correct. There is no significant overhead, however, Math.Pow also checks for decimals, so the simple multiplication wins 🙂

Dear Author, will you please provide me a tutorial for calculating the distance between two joints (out of 25) when a person is standing still?

Using Vitruvius, you can call the Length() extension method:

At the head of your file:

using LightBuzz.Vitruvius;

Inside the FrameArrived event:

var joint1 = body.Joints[JointType.KneeLeft];

var joint2 = body.Joints[JointType.KneeRight];

var distance = joint1.Length(joint2);

Just as an aside, it seems that for the Kinect V2 library, this would be:

At the head of your file:

using LightBuzz.Vitruvius;

Inside the FrameArrived event:

var joint1 = body.Joints[JointType.KneeLeft];

var joint2 = body.Joints[JointType.KneeRight];

var distance = MathExtensions.Length(joint1.Position,joint2.Position);

Thank you so much for your tutorial.

How about to find the distance between an object and Kinect? Here you used human body (player)

Hello Eric. If you’ve already detected an object in the 3D space using the depth frame array, you already know its distance from the sensor: it’s the ushort value of the depth array (measured in millimeters).

Hi sir, i am using kinect xbox 360 sensor with projector side by side, i want to calculate the projected screen dimensions. Can you please help me to find out.

Hello. I’m not sure I understand what you mean. The screen dimension of Kinect for XBOX 360 is 640×480.

Hi everybody

I am working with Kinect to track the face. I need to find the distance between face and Kinect sensor but I don’t know how can I do that.

I use lots of different command, but always I have some errors and some context that not exist on my code.

I tried also this method that is mentioned above, but it doesn’t work also.

Could you please help me?

Also I am using C# and I am a beginner in C#.

Honestly if I find the Depth, it will be enough for me.

This should work for you:

var head = body.Joints[JointType.Head].Position;

var distance = Length(head);

Thank you for your answer,

but I tried it before and it didn’t work.

because it doesn’t know body and it is just face tracker.

I have this error always:

Severity Code Description Project File Line Suppression State

Error CS0103 The name ‘body’ does not exist in the current context FaceTrackingBasics-WPF

Hi Ali. Are you using Kinect version 1 or 2? The code in this post targets Kinect version 2. Kinect version 2 always needs a tracked body to display facial information.

I am using Kinect 1.8

I assume you have the X/Y/Z coordinates of a face point. In that occasion, the Z value is the distance between the face point and the sensor.

yes I know.

But the problem is, how can I take the Z value(with which command)?

also my code doesn’t know Z?

Every face point is a set of X, Y, and Z coordinates. It should be yourPoint.Z or yourPoint.Position.Z.

Unfortunately my codes doesn’t know the Z as a coordinate.

finally I found the answer:

using (var skeletonFrame= e.OpenSkeletonFrame())

using (var depthFrame = e.OpenDepthImageFrame())

{

skeletonFrame.CopySkeletonDataTo(skeletons);

var skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];

foreach (var skeleton in skeletons)

{

if (skeleton.TrackingState != SkeletonTrackingState.Tracked) continue;

var head = skeleton.Joints[JointType.Head];

if (head.TrackingState == JointTrackingState.NotTracked) continue;

var depthImagePoint = depthFrame.MapFromSkeletonPoint(head.Position);

int depthInMillimeters = depthImagePoint.Depth; // TADA!

}

}

Hello Vangos,

Great tutorial,is the distance returned in meters.is there a way i can also detect the height of my hand.Does this code also work for SDK 1.8(xbox 360).

looking forward to your reply.

regards

Kh

Hi Kaushik. Yes, this works for Kinect 1.8, too. The distances are measured in meters.

Hello Vangos,

Thanks for ur response.i hope the same concept is used,if I want to measure the distance between my hand and the sensor.

Regards

KaushiK

That’s right 🙂

Hi,

I’m using matlab fro kinect (and some experiences of using C++) .

I wonder if I can connect two kinects and use them at the same time.(with using different buses for not having traffic problems)

Can you help me with that ?

Thanks in advance!

Hi Ali. You’ll need a separate computer to run the second Kinect. You cannot connect 2 Kinect sensors to the same computer.

Hello Vangos, Is there a way to measure the distance between the shoulder and arm of the human?

[…] one of my previous articles, I explained how to measure the distance between 2 points in the 3D space using simple Mathematical […]

my Final year project is 3D SLAM and i am using RTAB MAP for generating 3D MAP of unknoen environment can you please guide me how i can calculate distance between objects on a 3D map generated through RTAB MAP