# The right way to measure distances with the Kinect

- Posted: Aug 18, 2016 at 6:00AM
- 4 comments

Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Loading user information from MSDN

Something went wrong getting user information from MSDN

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

- Posted: Aug 18, 2016 at 6:00AM
- 4 comments

Vangos Pterneas, Kinect Hero, shares with us another awesome Kinect development post...

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

notthe linear distance between the point and the sensor. Instead,it’s the distance between the point and the! 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).planeof the sensorTo access the Z value, use the code below:

...

## 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:

...

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.

**Project Information URL:** http://pterneas.com/2016/08/11/measuring-distances-kinect/

Contact Information:

Follow @CH9

Follow @Coding4Fun

Follow @KinectWindows

Follow @gduncan411

**Already have a Channel 9 account?** Please sign in

## Follow the discussion

Oops, something didn't work.

## What does this mean?

Subscriptions allow us send you email notifications when new content is added. You need to be signed in to Channel 9 to use this feature.## What does this mean?

Subscriptions allow us send you email notifications when new content is added.sign up for email notifications?

It's very interesting, I have learned this at 12 years. So I'm a Kinect Hero too ???

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.

@Asterion: Thank you for your comment. This is exactly my point -- anybody can measure the distances using basic Maths we learnt in school! A lot of people, though, do not understand the difference between the distance and the Z depth distance. That's exactly why I tried to make it clear writing this post.

@birbilis: Correct!

## Remove this comment

## Remove this thread

Close