我有一个关于保存Kinect彩色视频流的问题。如何通过Kinect SDK 2在WPF中将Kinect的彩色视频流保存到硬盘上?我阅读了这个链接:Save Kinect's color camera video stream in to .avi video
对于一个项目,我必须这样做。我所做的可能不能满足您的所有要求,但可能会给您一个想法。首先,我将每个彩色帧图像保存在本地驱动器上,并进行连续命名。然后,使用ffmpeg将这些连续图像转换为视频文件,在我的情况下是mp4视频,而不是avi。
要按顺序保存彩色图像帧,您可以编写以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace Kinect_Video_Recorder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
KinectSensor ks;
ColorFrameReader cfr;
byte[] colorData;
ColorImageFormat format;
WriteableBitmap wbmp;
BitmapSource bmpSource;
int imageSerial;
public MainWindow()
{
InitializeComponent();
ks = KinectSensor.GetDefault();
ks.Open();
var fd = ks.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
uint frameSize = fd.BytesPerPixel * fd.LengthInPixels;
colorData = new byte[frameSize];
format = ColorImageFormat.Bgra;
imageSerial = 0;
cfr = ks.ColorFrameSource.OpenReader();
cfr.FrameArrived += cfr_FrameArrived;
}
void cfr_FrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
if (e.FrameReference == null) return;
using (ColorFrame cf = e.FrameReference.AcquireFrame())
{
if(cf == null) return;
cf.CopyConvertedFrameDataToArray( colorData, format);
var fd = cf.FrameDescription;
// Creating BitmapSource
var bytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel) / 8;
var stride = bytesPerPixel * cf.FrameDescription.Width;
bmpSource = BitmapSource.Create(fd.Width, fd.Height, 96.0, 96.0, PixelFormats.Bgr32, null, colorData, stride);
// WritableBitmap to show on UI
wbmp = new WriteableBitmap(bmpSource);
kinectImage.Source = wbmp;
// JpegBitmapEncoder to save BitmapSource to file
// imageSerial is the serial of the sequential image
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
using (var fs = new FileStream("./img/" + (imageSerial++) + ".jpeg", FileMode.Create, FileAccess.Write))
{
encoder.Save(fs);
}
}
}
}
}
PngBitmapEncoder
。ffmpeg.exe
进程,如下所示。
Process.Start("ffmpeg.exe", "-framerate 10 -i ./img/%d.jpeg -c:v libx264 -r 30 -pix_fmt yuv420p kinect_video.mp4");
注意:
希望能有所帮助 :)
wbmp = new WriteableBitmap(bmpSource);
如果你删掉它,用kinectImage.Source = bmpSource;
代替,就会好很多。 - AmiR Hossein