将Kinect Beta代码转换为1.5版

时间:2013-03-29 21:20:15

标签: c# vb.net sdk kinect

您好,我正在使用Kinect进行的项目。不幸的是,代码是测试版,我需要将其更新为1.5 sdk版本。我尝试了一些东西,但他们不会工作。这是我到目前为止所拥有的。该方法称为nui_DepthFrameReady。

 void nui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
    {
        if (!savedDepth)
        {
            PlanarImage Image = e.ImageFrame.Image;
            byte[] convertedDepthFrame = convertDepthFrame(Image.Bits);

            depth.Source = BitmapSource.Create(
                Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, convertedDepthFrame, Image.Width * 4);

            ++totalFrames;

            DateTime cur = DateTime.Now;
            if (cur.Subtract(lastTime) > TimeSpan.FromSeconds(1))
            {
                int frameDiff = totalFrames - lastFrames;
                lastFrames = totalFrames;
                lastTime = cur;
                frameRate.Text = frameDiff.ToString() + " fps";
            }

            if (subscribed)
            {
                //byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);
                //create an image based on the colored bytes
                BitmapSource myBitmapDepth = BitmapSource.Create(Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, convertedDepthFrame, Image.Width * PixelFormats.Bgr32.BitsPerPixel / 8);
                string imageFilePath = @"C:\Temp\kinect\depth\bmpDepthFrame_" + cur.TimeOfDay.ToString().Replace(':', '.') + ".png";
                string dataFilePath = @"C:\Temp\kinect\depth\depthFrame_" + cur.TimeOfDay.ToString().Replace(':', '.') + ".dat";
                //savePngFrame(myBitmapDepth, imageFilePath);

                //Crop frame to size 180x240
                byte[] croppedDepthFrame = new byte[180 * 240 * 2];

                for (int i = 0; i < 240; i++)
                {
                    for (int j = 0; j < 180 * 2; j += 2)
                    {
                        croppedDepthFrame[i * 180 * 2 + j] = Image.Bits[i * 320 * 2 + j + 69 * 2];
                        croppedDepthFrame[i * 180 * 2 + j + 1] = Image.Bits[i * 320 * 2 + j + 1 + 69 * 2];
                        //Console.Write((i * 180 * 2 + j) + "-" + (i * 180 * 2 + j + 69*2) +", ");
                    }
                    //Console.WriteLine();
                }
                FileStream fs = new FileStream(dataFilePath, FileMode.Create);
                fs.Write(croppedDepthFrame, 0, croppedDepthFrame.Length);
                fs.Close();
            }

            savedDepth = true;
        }
    }

感谢您的帮助。 这就是我到目前为止所拥有的

void nui_DepthImageReady(object sender, DepthImageFrameReadyEventArgs e)
    {
        if (!savedDepth)
        {
            short[] pixelData;
            bool receivedData = false;
            using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
            {
                if (depthImageFrame != null)
                {
                    if (pixelData == null) //allocate the first time
                    {
                        pixelData = new short[depthImageFrame.PixelDataLength];
                    }
                    depthImageFrame.CopyPixelDataTo(pixelData);
                    receivedData = true;
                }
                else
                {
                    // apps processing of image data took too long; it got more than 2 frames behind.
                    // the data is no longer avabilable.
                }
            }
            if (receivedData)
            {
                byte[] convertedDepthFrame = convertDepthFrame(Image.bits);

                depth.Source = BitmapSource.Create(Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, convertedDepthFrame, Image.Width * 4);

                ++totalFrames;

                DateTime cur = DateTime.Now;
                if (cur.Subtract(lastTime) > TimeSpan.FromSeconds(1))
                {
                    int frameDiff = totalFrames - lastFrames;
                    lastFrames = totalFrames;
                    lastTime = cur;
                    frameRate.Text = frameDiff.ToString() + " fps";
                }

                if (subscribed)
                {
                    //byte[] ColoredBytes = GenerateColoredBytes(e.ImageFrame);
                    //create an image based on the colored bytes
                    BitmapSource myBitmapDepth = BitmapSource.Create(Image.Width, Image.Height, 96, 96, PixelFormats.Bgr32, null, convertedDepthFrame, Image.Width * PixelFormats.Bgr32.BitsPerPixel / 8);
                    string imageFilePath = @"C:\Temp\kinect\depth\bmpDepthFrame_" + cur.TimeOfDay.ToString().Replace(':', '.') + ".png";
                    string dataFilePath = @"C:\Temp\kinect\depth\depthFrame_" + cur.TimeOfDay.ToString().Replace(':', '.') + ".dat";
                    //savePngFrame(myBitmapDepth, imageFilePath);

                    //Crop frame to size 180x240
                    byte[] croppedDepthFrame = new byte[180 * 240 * 2];

                    for (int i = 0; i < 240; i++)
                    {
                        for (int j = 0; j < 180 * 2; j += 2)
                        {
                            croppedDepthFrame[i * 180 * 2 + j] = Image.Bits[i * 320 * 2 + j + 69 * 2];
                            croppedDepthFrame[i * 180 * 2 + j + 1] = Image.Bits[i * 320 * 2 + j + 1 + 69 * 2];
                            //Console.Write((i * 180 * 2 + j) + "-" + (i * 180 * 2 + j + 69*2) +", ");
                        }
                        //Console.WriteLine();
                    }
                    FileStream fs = new FileStream(dataFilePath, FileMode.Create);
                    fs.Write(croppedDepthFrame, 0, croppedDepthFrame.Length);
                    fs.Close();
                }
            }
            savedDepth = true;
        }
    }

Image.Bits在kinect或Image.Width,Image.Height的新SDK中没有定义 这些是正在发生的错误,所以我不知道如何转换代码以获得相同的信息。

1 个答案:

答案 0 :(得分:0)

我正在将您的样本与Kinect 1.5 SDK示例中的示例进行比较,看起来您所谓的Image可能与pixelData相同。我没有看到Image定义在哪里,所以我猜。这有帮助吗?