【问题标题】:How to create a list of the most recent 45 image frames of a kinect如何创建最近 45 个 kinect 图像帧的列表
【发布时间】:2014-03-07 09:17:48
【问题描述】:

我正在尝试存储来自 kinect 的最新 45 色帧。我的想法是,我可以使用 kinect 使用的 colorPixel 数组以字节形式存储像素数据,并将其保存到我称为 Frames 的锯齿状数组中。下面的代码是 kinect 标准颜色查看器的事件处理程序的样子 `

//These are global parameter
next index = 0;
byte[45][] Frames;
byte nextIndex = 0; 

//This is the event handler within the code for a typical color viewer  
void sensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        //Opens the color frame sent from the event handler and declares it
        //as 'colorFrame'.
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            /*Check to see if colorFrame is null. It will be null if this frame cannot
            be processed before the event handler registers another frame. This
            results in the null frame being dropped as there is no method for 
            colorFrame == null.*/
            if (colorFrame != null)
            {            
                //Copies a frame of data to 'colorPixels'.
                colorFrame.CopyPixelDataTo(colorPixels);

                //Assigns colorPixels to the array Frames. Also, this will only store
                //the 45 most recent Frames
                Frames[nextIndex] = colorPixels;              
                nextIndex++;
                if (nextIndex == 45)
                {
                    nextIndex = 0;
                }               
                /*Updates the pixels in the specified region of the bitmap. Parameteres:
                Int32Rect- Describes the width, height, and location of an integer rectangle
                   XAML value for X coordinate of top left corner
                   XAML value for Y coordinate of top left corner
                   Width of the rectangle
                   Heigth of the rectangle
                Pixels- the pixel array used to update the bitmap
                Stride- the stride of the update region in pixels
                Offset- the input buffer offset */
                colorBitmap.WritePixels(
                   new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight),
                   colorPixels,
                   colorBitmap.PixelWidth * sizeof(int),
                   0);
             }
        }
    }`

该代码的问题在于,当它运行时,它将当前正在处理的数组 (colorPixels) 分配给 Frames 数组的每个元素。因此,来自第一帧的第一个数组 colorPixels 存储为 Frames[0],但是当事件处理程序再次触发并且第二个 colorPixels 数组准备就绪时,Frames[0] 和 Frames[1] 都被重新分配为 colorPixel 数组第 2 帧。谢谢!

【问题讨论】:

    标签: c# arrays kinect assign


    【解决方案1】:

    您只有一个字节缓冲区,并将对该单字节缓冲区的引用分配给几个数组元素。

    我建议使用字节缓冲区列表而不是多维数组。这样会更容易阅读。

    首先:删除你的字节缓冲区colorPixels的定义并替换为

    const int frameCount = 45;    
    List<byte[]> frames;
    

    第二:你必须初始化你的缓冲区。在您的代码中的某处,您使用

    之类的东西初始化字节缓冲区
    this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];
    

    用类似的东西来初始化你的字节缓冲区:

    frames = new List<byte[]>(frameCount);
    for (int i = 0; i < frameCount; i++)
    {
       frames.Add(new byte[this.sensor.ColorStream.FramePixelDataLength]);
    }
    

    第三:使用事件处理程序中的缓冲区。

    byte[] colorPixels = frames[nextIndex];
    if (++nextIndex >= frameCount)
    {
       nextIndex = 0;
    }
    
    colorFrame.CopyPixelDataTo(colorPixels);
    colorBitmap.WritePixels(...
    

    【讨论】:

    • 请注意,您可以使用具有已定义容量的Lists
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2014-12-25
    • 2012-10-07
    相关资源
    最近更新 更多