Kinect 2快速手柄框架 - >低fps率 [英] Kinect 2 fast handle frames -> low fps rate

查看:67
本文介绍了Kinect 2快速手柄框架 - >低fps率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用kinect 2的示例颜色基础并计算每个图像之间的毫秒时间时,它通常约为30ms,但是当我将函数图像事件的内部代码更改为注释时,帧之间的时间是:

When I use the example color basics of the kinect 2 and calculate time in millisecond between each image, it's usually around 30ms, but when I change the inner code of the function image event into comment, time between frames are:

34,231,32,33,134,32,266,32,33,172,67,166,28,64,33,101,32,33,34,32,12,138,94,32,26 ..

34,231,32,33,134,32,266,32,33,172,67,166,28,64,33,101,32,33,34,32,32,138,94,32,26..

public partial class MainWindow : Window, INotifyPropertyChanged
        {
            /// <summary>
            /// Active Kinect sensor
            /// </summary>
            private KinectSensor kinectSensor = null;
    
            /// <summary>
            /// Reader for color frames
            /// </summary>
            private ColorFrameReader colorFrameReader = null;
    
            /// <summary>
            /// Bitmap to display
            /// </summary>
            private WriteableBitmap colorBitmap = null;
    
            /// <summary>
            /// Current status text to display
            /// </summary>
            private string statusText = null;
    
            /// <summary>
            /// Initializes a new instance of the MainWindow class.
            /// </summary>
            public MainWindow()
            {
                // get the kinectSensor object
                this.kinectSensor = KinectSensor.GetDefault();
    
                // open the reader for the color frames
                this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();
    
                // wire handler for frame arrival
                this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;
    
                // create the colorFrameDescription from the ColorFrameSource using Bgra format
                FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
    
                // create the bitmap to display
                this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
    
                // set IsAvailableChanged event notifier
                this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;
    
                // open the sensor
                this.kinectSensor.Open();
    
                // set the status text
                this.StatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText
                                                                : Properties.Resources.NoSensorStatusText;
    
                // use the window object as the view model in this simple example
                this.DataContext = this;
    
                // initialize the components (controls) of the window
                this.InitializeComponent();
    
                sw.Start();
            }
    
            Stopwatch sw = new Stopwatch();
            /// <summary>
            /// Handles the color frame data arriving from the sensor
            /// </summary>
            /// <param name="sender">object sending the event</param>
            /// <param name="e">event arguments</param>
            private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
            {
                System.Console.WriteLine(sw.ElapsedMilliseconds);
                sw.Restart();
                // ColorFrame is IDisposable

                // Changed into comment here:

                /*using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
                {
                    if (colorFrame != null)
                    {
                        FrameDescription colorFrameDescription = colorFrame.FrameDescription;
    
                        using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
                        {
                            this.colorBitmap.Lock();
    
                            // verify data and write the new color frame data to the display bitmap
                            if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight))
                            {
                                colorFrame.CopyConvertedFrameDataToIntPtr(
                                    this.colorBitmap.BackBuffer,
                                    (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4),
                                    ColorImageFormat.Bgra);
    
                                this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight));
                            }
    
                            this.colorBitmap.Unlock();
                        }
                    }
                }*/
            }
        }

I用秒表检查这两种情况

I check the both cases with Stopwatch


当我"花钱"时在这个函数上有足够的时间它可以完美地添加:

When I "spend" enough time on this function it work perfect like adding:

for (int i = 0 ; i < 1000000; i++);



我不记得我是否使用了1 mil或10 mil迭代的循环(并且它取决于计算机),但在使用之后我几乎总是每30ms帧。

I don't remember if I used loop of 1 mil or 10 mil iterations (and it's depend the computer), but after use that I get almost always frame every 30ms.


"花费"时间使用  thread.sleap < span style ="color:#222222; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:15px; line-height:19.5px"> 不会
修复它作为添加循环

为什么以及如何解决它?

Why that and how to solve it?

推荐答案

偶然的事情,因为你没有释放被通知的帧,当你得到另一个帧时,运行时会延迟,因为你不尊重合同。添加最小值添加using语句,以便释放提供给
you的帧。

couple things, because you are not releasing the frame that you were notified, the runtime is delaying when you get another frame since you are not respecting the contract. Add a minimum add the using statement so you are releasing the frame provided to you.

using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) {}

如果您想使用轮询,请不要订阅活动,只是在需要时继续请求框架。

If you want to use polling, don't subscribe to events and just keep requesting frames when you want them.


这篇关于Kinect 2快速手柄框架 - &gt;低fps率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆