【问题标题】:Capturing Kinect Depth Image to a png file将 Kinect 深度图像捕获为 png 文件
【发布时间】:2012-08-31 08:05:12
【问题描述】:

我正在尝试将 kinect 深度传感器图像保存到 png 文件中。我试图用我的 RGB 相机拍摄它,它没有任何问题。有什么我错过的吗? 附言我正在使用 Kinect ToolKit 的函数来显示 rgb 和深度图像。

WriteableBitmap depthBitmap;
DepthStreamManager dsm;
dsm = new DepthStreamManager();
kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
kinect.DepthFrameReady += kinect_DepthFrameReady;
depthBitmap = new WriteableBitmap(kinect.DepthStream.FrameWidth,this.kinect.DepthStream.FrameHeight, 96.0, 96.0, PixelFormats.Gray16, null);
private string TakeImage(int x)
    {
        if (x == 0)
        {
            BitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(this.depthBitmap));
            string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string path = System.IO.Path.Combine(myPhotos, "Image 1.png");
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    encoder.Save(fs);
                }


            }
            catch (IOException details)
            {
                Console.Write(details.ToString());

            }
            if (path == null)
                return "Image was not taken.";
            else
                return path;
        }}

【问题讨论】:

    标签: c# image kinect


    【解决方案1】:

    如果你使用Coding4Fun Kinect toolkit,相信你可以使用ImageFrame对象。您可以转换ImageFrame object to Bitmap,然后您应该可以毫无问题地转换为save it as a PNG。

    解决方案有点冗长,但我目前没有使用装有我的 Kinect 东西的机器。

    编辑:如果您使用的是 WinForms,您可以简单地使用 Coding4Fun 工具包方法 ImageFrame.ToBitmap(),然后从那里保存为 PNG。

    代码应如下所示:

    private void saveAsPNG(ImageFrame myImageFrame, string path)
    {
       Bitmap bmp = myImageFrame.ToBitmap();
    
       bmp.Save(path, System.Drawing.Imaging.ImageFormat.Png);
    
       bmp.Dispose();
    }
    

    【讨论】:

    • 我试过这个,但我似乎不适用于 wpf。我之前尝试过 Coding4Fun 的东西,但我搬走了,因为它对于某些东西来说确实不够详细。
    • 你说得对,Coding4Fun 库只适用于基本的东西。我最近在 Kinect 项目中使用了 WPF,对于一些简单的事情来说,这可能是一种痛苦。就在我的脑海中,我认为我不能扩大我的答案来帮助你。祝你好运。
    • 您使用的是我建议的解决方案还是其他解决方案?如果您使用另一种解决方案,我很乐意看到它,我相信其他人也会。 编辑:抱歉,我没有看到你已经发布了它。谢谢:)
    【解决方案2】:

    解决我自己的问题:

    使用 Kinect 的 ToolKit,他们实际上提供了一个可写位图函数,允许我抓取 ColorStreamManager 或 DepthStreamManager 的 WritableBitmap。我之前的错误是因为我没有使用 Kinect 的先天显示方法,所以 writeablebitmap 肯定是空的。这是我的固定密码。

    WriteableBitmap wmp;
                wmp = cis.Bitmap;
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(wmp));
                string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"Image 1.png");
                try
                {
                    using (FileStream fs = new FileStream(path, FileMode.Create))
                    {
                        encoder.Save(fs);
                    }
                }
                catch (IOException ioe)
                {
                    Console.WriteLine(ioe.ToString());
                }
                return path;
    

    【讨论】:

    • wmp=cis.Bitmap; 中的 cis 是什么,它是某个库中的任何类对象吗...?如果是这样,哪个图书馆?以及solutioExplorer中的参考..?抱歉问了这么一个基本问题,但我是这个领域的新手..
    【解决方案3】:

    您甚至不需要 WriteableBitmap。只是带有 BitmapSource 的 编码器,其中包含 pixeldata。

    using Microsoft.Kinect;
    using System.Windows.Media; // WindowsBase
    using System.Windows.Media.Imaging; //PresentationCore
    

    ...

    static void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
        {
    
            DepthImageFrame DIF = e.OpenDepthImageFrame();
            if (DIF == null){return;}            
    
            short[] pixels = new short[DIF.PixelDataLength];
            DIF.CopyPixelDataTo(pixels);
    
            if (check == 0)
            {
                int stride = DIF.Width * DIF.BytesPerPixel;
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(BitmapSource.Create(
                    DIF.Width, DIF.Height, 96, 96, PixelFormats.Gray16, null, pixels, stride)));
    
                string path = System.IO.Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "kimage.png");
    
                try
                {
                    using (FileStream fs = new FileStream(path, FileMode.Create))
                    {
                        encoder.Save(fs);
                    }
                }
                catch (IOException ioe)
                {
                    Console.WriteLine(ioe.ToString());
                }
                check = 1;
            }}
    

    环境:

    • Visual Studio 2012 速成版
    • Microsoft.Kinect 库 1.7.0.0
    • 编码器需要组装System.Xaml

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2014-11-17
      • 2013-11-24
      相关资源
      最近更新 更多