如何通过WPF中的Kinect SDK 2将kinect彩色视频保存为硬盘上的流 [英] How to save kinect color video as stream on hard disk by Kinect SDK 2 in WPF

查看:140
本文介绍了如何通过WPF中的Kinect SDK 2将kinect彩色视频保存为硬盘上的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于将kinect彩色视频另存为流的问题 如何通过WPF中的Kinect SDK 2将Kinect的彩色视频作为流保存到硬盘中??

I have a question about save kinect color video as stream How to save Kinect's color video as stream to hard by Kinect SDK 2 in WPF???

我阅读了此链接:保存Kinect的彩色相机视频流到.avi视频

推荐答案

对于一个项目,我必须这样做.我所做的可能无法满足您的所有要求,但可能会给您一个想法.最初,我使用顺序命名将每个色框图像保存在本地驱动器中.然后使用ffmpeg将这些顺序图像转换为视频文件,在我的情况下是mp4视频,而不是avi.

For a project, I had to do this. What I've done may not fulfill all your requirements but it may give you an idea. At first I saved every color frame image in local drive with a sequencial naming. Then with ffmpeg I converted those sequential image to video file, in my case it was mp4 video, not avi.

要顺序保存彩色图像帧,您可以像下面这样编码,

To save color image frame sequentially, you may code like below,

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

以上示例将图像保存为jpeg格式.如果您需要将其另存为png格式,请使用

Above example saves the images in jpeg format. if you need to save it as png format use PngBitmapEncoder.

现在,我们已将顺序图像保存在硬盘中.要将这些顺序图像转换为视频文件,可以使用 ffmpeg .您也可以使用 Aforge.net .但是我从来没有使用过它.就我而言,我从C#程序中调用了ffmpeg.exe进程,如下所示.

Now we have saved sequential images in hard drive. To convert these sequential images to video file you can use ffmpeg. You can also use Aforge.net. But I've never used it. In my case I called the ffmpeg.exe process from my C# program like below.

Process.Start("ffmpeg.exe", "-framerate 10 -i ./img/%d.jpeg -c:v libx264 -r 30 -pix_fmt yuv420p kinect_video.mp4");

注意:

  1. 将构建目标设为x64.这将增加程序的内存限制.
  2. 我已经为此编写了一个基本的实现.您可以查看是否需要.
  1. Make your Build target x64. This will increase the memory limit of the program.
  2. I've coded a basic implementation regarding this. You can check out if you want.

希望它会有所帮助:)

这篇关于如何通过WPF中的Kinect SDK 2将kinect彩色视频保存为硬盘上的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆