将Kinect测试版代码转换为1.5版



嗨,我在做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:TempkinectdepthbmpDepthFrame_" + cur.TimeOfDay.ToString().Replace(':', '.') + ".png";
                string dataFilePath = @"C:TempkinectdepthdepthFrame_" + 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:TempkinectdepthbmpDepthFrame_" + cur.TimeOfDay.ToString().Replace(':', '.') + ".png";
                    string dataFilePath = @"C:TempkinectdepthdepthFrame_" + 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;
        }
    }

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

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

最新更新