Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get Depth(Z position) of specified coordinate with Kinect v2
public partial class MainWindow : Window
private KinectSensor kinectSensor = null;
private CoordinateMapper coordinateMapper = null;
private MultiSourceFrameReader multiFrameSourceReader = null;
public MainWindow()
// one sensor is currently supported
this.kinectSensor = KinectSensor.GetDefault();
// get the coordinate mapper
this.coordinateMapper = this.kinectSensor.CoordinateMapper;
// get the depth (display) extents
FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
FrameDescription frameDescription = this.kinectSensor.DepthFrameSource.FrameDescription;
// open multiframereader for depth, color, and bodyindex frames
this.multiFrameSourceReader = this.kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color | FrameSourceTypes.Body);
this.multiFrameSourceReader.MultiSourceFrameArrived += this.Reader_MultiSourceFrameArrived;
private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();
if (multiSourceFrame != null)
using (DepthFrame depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame())
if (depthFrame != null)
// Specified X, Y coordinate
// In 1920 x 1080 color frame
double x = 1000;
double y = 900;
FrameDescription depthFrameDescription = depthFrame.FrameDescription;
depthWidth = depthFrameDescription.Width;
depthHeight = depthFrameDescription.Height;
depthframeData = new ushort[depthWidth * depthHeight];
CameraSpacePoint[] csp = new CameraSpacePoint[1920 * 1080];
this.coordinateMapper.MapColorFrameToCameraSpace(depthframeData, csp);
// Depth(Z Position) of specified coordinate
float DepthPosition = csp[(1920 * Convert.ToInt16(y)) + Convert.ToInt16(x)].Z;

This comment has been minimized.

Copy link
Owner Author

@taylor224 taylor224 commented Mar 12, 2015

Get Depth of specified coordinate with Kinect V2

Use this when you want to get depth(Z coordinate) of specified X,Y coordinate.
It will return float Z coordinate.
Z value means meter distance from Kienct sensor.
1 meter = 1.0 / 2.5 meter = 2.5
If Z value is zero, It means Kinect sensor could not get distance of that coordinate.
Max X coordinate is 1920 and Max Y coordinate is 1080.
X,Y coordinate is referenced by ColorFrame(1920x1080) not DepthFrame(512x424).

This source is part of WPF program. Modify it to fit your program.
MapColorFrameToCameraSpace() function can make your program update slowly. Please notice about this.

You need Kinect for Windows V2 sensor, Kinect SDK 2.0 and Visual Studio 2013 to use this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment