Capturing a Kinect Depth Image in a PNG File

I am trying to save a kinect depth sensor image in a png file. I tried to take this with my RGB camera and it had no problems. Is there something I missed? Postscript I use the Kinect ToolKit functions to display rgb images and depth.

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; }} 
+4
source share
3 answers

Solving my own problem:

Using the Kinect ToolKit, they actually provided a writable bitmap function that allows me to capture the ColorStreamManager or DepthStreamManager WritableBitmap. My mistake previously was that since I did not use Kinect's built-in mapping methods, writeablebitmap would definitely be empty. These are my fixed codes.

 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; 
+2
source

If you use the Coding4Fun Kinect toolkit , I believe you can use ImageFrame objects. You can convert the ImageFrame to Bitmap , and from there you can save it as a PNG without any problems.

A bit of a long route to a solution, but I currently do not use a machine that has my Kinect materials on.

Edit: if you use WinForms, you can simply use the Coding4Fun ImageFrame.ToBitmap () toolkit method, and then save the PNG to it.

The code should look something like this:

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

You don't even need WriteableBitmap. Just an encoder with a BitmapSource that contains pixel strings.

 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; }} 

WEDNESDAY:

  • Visual studio 2012 express
  • Microsoft.Kinect 1.7.0.0 library
  • Encoder needs assembly System.Xaml
0
source

All Articles