Search code examples
objectopencvkinecttrackingdepth

Getting Depth of other objects Kinect


I'm a newbie in kinect programming, i am working on a ball tracking using kinect and opencv..we all know that kinect provides Depth data, and with the code below:

DepthImagePoint righthandDepthPoint = 
    sensor.CoordinateMapper.MapSkeletonPointToDepthPoint
    (
        me.Joints[JointType.HandRight].Position, 
        DepthImageFormat.Resolution640x480Fps30
    );

double rightdepthmeters = (righthandDepthPoint.Depth);

using this, I am able to get the depth of a right hand, using the function MapSkeletonPointToDepthPoint() by specifing the jointtype..

Is it possible to get the depth of other objects by specifying in the image where? given the coordinate..I want to get the depth of the object in that coordinate?


Solution

  • Pulling depth data from the Kinect SDK can be extracted from the DepthImagePixel structure.

    The example code below loops through the entire DepthImageFrame to examine each of the pixels. If you have a specific coordinate you wish to look at, remove the for loop and set the x and y to a specific value.

    // global variables
    
    private const DepthImageFormat DepthFormat = DepthImageFormat.Resolution320x240Fps30;
    private const ColorImageFormat ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;
    
    private DepthImagePixel[] depthPixels;
    
    // defined in an initialization function
    
    this.depthWidth = this.sensor.DepthStream.FrameWidth;
    this.depthHeight = this.sensor.DepthStream.FrameHeight;
    
    this.depthPixels = new DepthImagePixel[this.sensor.DepthStream.FramePixelDataLength];
    
    private void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e)
    {
        if (null == this.sensor)
            return;
    
        bool depthReceived = false;
    
        using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
        {
            if (null != depthFrame)
            {
                // Copy the pixel data from the image to a temporary array
                depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
    
                depthReceived = true;
            }
        }
    
        if (true == depthReceived)
        {
            // loop over each row and column of the depth
            for (int y = 0; y < this.depthHeight; ++y)
            {
                for (int x = 0; x < this.depthWidth; ++x)
                {
                    // calculate index into depth array
                    int depthIndex = x + (y * this.depthWidth);
    
                    // extract the given index
                    DepthImagePixel depthPixel = this.depthPixels[depthIndex];
    
                    Debug.WriteLine("Depth at [" + x + ", " + y + "] is: " + depthPixel.Depth);
                }
            }
        }
    }