[英]How to save a WriteableBitmap obejct retained from Kinect v2 to an image file in Windows Store apps
我有一个Windows应用程序c#程序,该程序通过Kinect v2红外摄像头检测到用户头部的位置。
红外数据保存在WriteableBitmap中,我想将检测到的头部保存为HD上的图像文件。
我已经尝试了很多这样的解决方案, 但是都没有运气。
这是我的头部追踪代码。 irBitmap是我的WritableBitmap,一旦检测到头部,我想将其保存为图像文件。
void msfr_MultiSourceFrameArrived(MultiSourceFrameReader sender, MultiSourceFrameArrivedEventArgs args)
{
using (MultiSourceFrame msf = args.FrameReference.AcquireFrame()) //acquiring frame.
{
if (msf != null)
{
using (BodyFrame bodyFrame = msf.BodyFrameReference.AcquireFrame()) //acquire the body frame now in the msf.
{
using (InfraredFrame irFrame = msf.InfraredFrameReference.AcquireFrame())
{
if (bodyFrame != null && irFrame != null)
{
irFrame.CopyFrameDataToArray(irData);
for (int i = 0; i < irData.Length; i++)
{
byte intensity = (byte)(irData[i] >> 8);
irDataConverted[i * 4] = intensity;
irDataConverted[i * 4 + 1] = intensity;
irDataConverted[i * 4 + 2] = intensity;
irDataConverted[i * 4 + 3] = 255;
}
irDataConverted.CopyTo(irBitmap.PixelBuffer);
irBitmap.Invalidate();
bodyFrame.GetAndRefreshBodyData(bodies);
bodyCanvas.Children.Clear();
foreach (Body body in bodies) //now we go trough each of our bodies (in order to look for head)
{
if (body.IsTracked) //Chek if the body is tracked
{
Joint headJoint = body.Joints[JointType.Head]; //If body is tracked we will get the head joint
if (headJoint.TrackingState == TrackingState.Tracked) //We need to make sure with this bool that the head is tracked.
{
DepthSpacePoint dps = sensor.CoordinateMapper.MapCameraPointToDepthSpace(headJoint.Position); //We create a debthspace point so we can draw that into our image. This is drawin in the image. It is the cameraspace point that goes to debthspace.
Ellipse headCircle = new Ellipse() {Width = 100, Height = 100, Fill = new SolidColorBrush(Color.FromArgb(255,255,0,0))}; //Ellispe to draw the new heads.
bodyCanvas.Children.Add(headCircle); //Add the headcircle drawn circle to the canvas.
Canvas.SetLeft(headCircle, dps.X-50); //and now we tell the canvas where to draw this shit.
Canvas.SetTop(headCircle, dps.Y-50); //
}
}
}
}
}
}
}
}
}
对于如何将WritableBitmap作为图像文件保存在硬盘上的任何建议/帮助/建议,我将不胜感激。
我已将以下代码用于类似的项目。 在这里,数据是来自kinect的原始图像流。
MemoryStream ms = new MemoryStream(data);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
img.Save("./image" + (++imageSerial) + ".png", System.Drawing.Imaging.ImageFormat.Png);
感谢您的回答。 我设法使用以下代码保存图像:
StorageFile sampleFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(fileName , CreationCollisionOption.GenerateUniqueName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.