将Kinect深度图像捕获到png文件中

4

我正在尝试将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;
        }}
3个回答

2

解决我的问题的方法:

使用Kinect的工具包,他们实际上提供了一个可写位图函数,允许我抓取ColorStreamManager或DepthStreamManager的WritableBitmap。 我以前的错误是因为我没有使用Kinect的本机显示方法,因此可写位图肯定为空。 这是我的修复代码。

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 是什么?它是来自某个库的任何类对象吗? 如果是,那么是哪个库?还有在 Solution Explorer 中的引用...? 对不起问这样一个基础的问题,但我是新手... - kAmol

2
如果您使用Coding4Fun Kinect toolkit,我相信您可以使用ImageFrame对象。您可以将ImageFrame对象转换为位图,然后您应该能够将其保存为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的东西,但我放弃了,因为对于某些东西来说,它真的不够详细。 - Bocky
你说得对,Coding4Fun库只适用于基本的东西。最近我在一个Kinect项目中使用了WPF,但有时候它会让一些简单的事情变得非常麻烦。就我目前所知,我无法进一步扩展我的答案来帮助你。祝你好运。 - bobble14988
1
你使用了我建议的解决方案还是其他的?如果你使用了其他的解决方案,我很想看看它,而且我相信其他人也是。 编辑:抱歉,我没有看到你已经发布了解决方案。谢谢 :) - bobble14988

0
你甚至不需要WriteableBitmap。只需要带有pixeldataBitmapSourceencoder即可。
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 express
  • Microsoft.Kinect Library 1.7.0.0
  • 编码器需要 System.Xaml 程序集

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接