Search code examples
c#image-processinguwpkinectdepth-buffer

Kinect V2 UWP How to calculate distance from user


I am trying to calculate distance from a person to the the Kinect sensor v2 in UWP c#.

In WPF, I was getting this by

double distanceMm = this.bodies[i].Joints[JointType.Head].Position.Z * 1000; // meters to millimetres

In UWP using depth frame, I am able to get minimum and maximum reliable distance, but I am not sure how to get the user's distance to Kinect sensor.

var depthScaleInMeters = vidFrame.DepthMediaFrame.DepthFormat.DepthScaleInMeters;
var depthRangeMinimumInMeters = vidFrame.DepthMediaFrame.MinReliableDepth * depthScaleInMeters;
var depthRangeMaximumInMeters = vidFrame.DepthMediaFrame.MaxReliableDepth * depthScaleInMeters;

Can someone please help?

Edit:

I found the x and y cords of the face/object in Depth Image and got its depth value but when I try to convert it to distance in meters, it seems to be alot less value than in real world (this should be 1.2 meters).

int x = 235;//specify x value here
int y = 215;//specify y value here
int d = (ushort)output[x + y * PixelHeight] ; //PixelHeight  = 512
d = d >> 3;//this is distance in mm
double metersdistance = d  * 0.001; // meters

Solution

  • I used face detection to get face coordinates from colour image. Mapped those coordinates on IR and depth frame. This way, I found the x and y cords of the face from depth frame.

    int x = 235;//specify x value here
    int y = 215;//specify y value here
    int d = (ushort)output[x + y * PixelHeight] ; //PixelHeight  = 512
    double metersdistance = d  * 0.001; // meters
    

    d gave the depth value and multiplying it by 0.001 converted it to meters. I tested this for distance between 1meter to 3 meter (at different positions in the room) and it seemed to give me same readings as of real world. For now, this seems to work.