Search code examples
c#kinectframe-rate

kinect data acquiring fps>30


I was trying to Get and Display Depth Data in C# and calculate fps for depth acquisition for kinect.

To calculate fps for depth implemented a datetime

  if (this.sensor != null)
  {
      this.sensor.DepthFrameReady += this.DepthImageReady;
  }

  private void DepthImageReady(object sender, DepthImageFrameReadyEventArgs e)
  {
     DateTime before = DateTime.Now;
      using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
      {
          if (depthFrame != null)
          {
              depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
          }
          else
          {
              // depthFrame is null because the request did not arrive in time
          }
      }
     DateTime after = DateTime.Now;
     TimeSpan result = after.Subtract(before);
        float seconds = (float)result.TotalSeconds;
        this.Text = "Kinect (" + (1 / seconds) + "fps)";

  }

I am getting >60 fps and unbelievably infinity sometime

While kinect gives 30 fps why am i getting infinity, what's wrong am I doing?


Solution

  • You have to measure the interval between each time your function is called, not how long it takes for your function to execute. Something like this:

    static DateTime lastFrame = DateTime.Now;
    private void DepthImageReady(object sender, DepthImageFrameReadyEventArgs e)
    {
        using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
        {
            if (depthFrame != null)
            {
                depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
            }
            else
            {
                // depthFrame is null because the request did not arrive in time
            }
        }
        var now = DateTime.Now;
        TimeSpan result = now.Subtract(lastFrame);
        lastFrame = now;
        var milliseconds = result.TotalMilliseconds;
    
        this.Text = "Kinect (" + (1000.0 / milliseconds) + "fps)";
    }