我正在尝试将kinect深度传感器图像保存到png文件中。我试着用我的RGB相机拍了这个,它没有任何问题。有什么东西我错过了吗?附言:我正在使用Kinect工具箱的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;
}}
如果您使用Coding4Fun Kinect工具包,我相信您可以使用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();
}
我自己问题的解决方案:
使用Kinect的工具箱,他们实际上提供了一个可写位图功能,可以让我获取ColorStreamManager或DepthStreamManager的可写位图。我之前的错误是,由于我没有使用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;
您甚至不需要可写位图。只有编码器和保存像素数据的位图源。
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