[英]Capturing Kinect Depth Image to a png file
I am trying to save a kinect depth sensor image to a png file. 我正在尝试将kinect深度传感器图像保存到png文件中。 I tried to take this with my RGB camera and it didn't have any problems.
我尝试用我的RGB相机拍摄它并没有任何问题。 Is there something that I missed out on?
有什么东西我错过了吗? PS I am using Kinect ToolKit's functions for both rgb and depth image display.
PS我正在使用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;
}}
If you use the Coding4Fun Kinect toolkit , I believe you can use ImageFrame objects. 如果您使用Coding4Fun Kinect工具包 ,我相信您可以使用ImageFrame对象。 You can convert an ImageFrame object to Bitmap and from there you should be able to save it as a PNG with no problems.
您可以将ImageFrame对象转换为Bitmap,然后您可以将其保存为PNG而不会出现问题。
Bit of a long-winded route to the solution but I'm not currently using the machine which has my Kinect stuff on it. 这个解决方案有点冗长的路线,但我目前还没有使用有我的Kinect东西的机器。
Edit: If you're using WinForms, you can simply use the Coding4Fun toolkit method ImageFrame.ToBitmap() and then save as PNG from there. 编辑:如果您正在使用WinForms,您只需使用Coding4Fun工具包方法ImageFrame.ToBitmap(),然后从那里保存为PNG。
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();
}
Solution to my own problem: 解决我自己的问题:
Using Kinect's ToolKit, they actually supplied a writable bitmap function that allows me to grab the ColorStreamManager or DepthStreamManager's WritableBitmap out. 使用Kinect的ToolKit,他们实际上提供了一个可写的位图函数,它允许我抓取ColorStreamManager或DepthStreamManager的WritableBitmap。 My error previously was that since I wasn't using Kinect's innate display methods, the writeablebitmap will definitely be empty.
我之前的错误是因为我没有使用Kinect的先天显示方法,所以writeablebitmap肯定是空的。 This is 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;
You don't even need the WriteableBitmap . 你甚至不需要WriteableBitmap 。 Just the encoder with a BitmapSource that holds the pixeldata .
只是带有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;
}}
ENVIRONMENT: 环境:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.