【问题标题】:How can record RGB video from Kinect?如何从 Kinect 录制 RGB 视频?
【发布时间】:2014-03-04 09:48:00
【问题描述】:

我正在尝试从 Kinect 捕获 RBG 视频。

我已经用 C# 编写了这段代码,但我不知道如何将帧保存在 avi/mpeg 文件中。 在第一种方法中,我为 rgb 视频设置传感器,在第二种方法中,我从 kinect 传感器接收帧。

void RecordVideo()
{
    foreach (var kinectCollegata in KinectSensor.KinectSensors)
    {
        if (kinectCollegata.Status == KinectStatus.Connected)
        {
            this.sensor = kinectCollegata;
            break;
        }
    }

    if (this.sensor != null)
    {
        this.sensor.SkeletonFrameReady += this.SensorSkeletonFrameReady;

        this.sensor.Start();
        var parameters = new TransformSmoothParameters
        {
            Smoothing = 0.3f,
            Correction = 0.0f,
            Prediction = 0.0f,
            JitterRadius = 1.0f,
            MaxDeviationRadius = 0.5f
        };

        this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);

        this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
        sensor.Start();
    }
}

private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    try
    {
        Skeleton[] skeletons = new Skeleton[0];
        SkeletonFrame skeletonFrame;
        using (skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
        }
    }
    catch (Exception exc)
    {
    }
}

如何修改用于在光盘上写入视频文件的代码?


谢谢你的回复,我已经做到了。

1)

this.sensor.ColorFrameReady += this.ColorImageReady;

2)

private void ColorImageReady(object sender, ColorImageFrameReadyEventArgs e)
{
   using (ColorImageFrame imageFrame = e.OpenColorImageFrame())
   {
      if (imageFrame != null)
      {
         Bitmap image = ImageToBitmap(imageFrame);
         int width = 320;
         int height = 240;
         // create instance of video writer
         VideoFileWriter writer = new VideoFileWriter();
         // create new video file
         writer.Open("test.avi", width, height, 25, VideoCodec.MPEG4);
         // create a bitmap to save into the video file
         for (int i = 0; i < 1000; i++)
         {
            image.SetPixel(i % width, i % height, System.Drawing.Color.Red);
            writer.WriteVideoFrame(image);
         }
         writer.Close();
         }
     }
  }

        Bitmap ImageToBitmap(ColorImageFrame Image)
        {
            byte[] pixeldata = new byte[Image.PixelDataLength];
            Image.CopyPixelDataTo(pixeldata);
            Bitmap bmap = new Bitmap(Image.Width, Image.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            BitmapData bmapdata = bmap.LockBits(
            new System.Drawing.Rectangle(0, 0, Image.Width, Image.Height),
            ImageLockMode.WriteOnly,
                bmap.PixelFormat);
            IntPtr ptr = bmapdata.Scan0;
            Marshal.Copy(pixeldata, 0, ptr, Image.PixelDataLength);
            bmap.UnlockBits(bmapdata);
            return bmap;
        }

但是当我运行我的应用程序时出现了这个异常:

System.IO.FileNotFoundException non è stata gestita
  _HResult=-2147024770
  _message=Impossibile caricare il file o l'assembly 'AForge.Video.FFMPEG.dll' o una delle relative dipendenze. Impossibile trovare il modulo specificato.
  HResult=-2147024770
  IsTransient=false
  Message=Impossibile caricare il file o l'assembly 'AForge.Video.FFMPEG.dll' o una delle relative dipendenze. Impossibile trovare il modulo specificato.
  Source=Disfagia
  FileName=AForge.Video.FFMPEG.dll
  FusionLog=""
  StackTrace:
       in Disfagia.MainWindow.ColorImageReady(Object sender, ColorImageFrameReadyEventArgs e)
       in Microsoft.Kinect.ContextEventHandler`1.SendOrPostDelegate(Object state)
       in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       in MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       in System.Windows.Threading.DispatcherOperation.InvokeImpl()
       in System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Windows.Threading.DispatcherOperation.Invoke()
       in System.Windows.Threading.Dispatcher.ProcessQueue()
       in System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       in MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       in System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       in MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       in MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       in System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       in System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       in System.Windows.Threading.Dispatcher.Run()
       in System.Windows.Application.RunDispatcher(Object ignore)
       in System.Windows.Application.RunInternal(Window window)
       in System.Windows.Application.Run(Window window)
       in System.Windows.Application.Run()
       in Disfagia.App.Main() in c:\Users\michele.castriotta\Documents\Visual Studio 2013\Projects\Disfagia\Disfagia\obj\Debug\App.g.cs:riga 0
       in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Threading.ThreadHelper.ThreadStart()
  InnerException:

【问题讨论】:

    标签: c# kinect rgb


    【解决方案1】:

    尝试以下方法:

    • 从传感器订阅ColorFrameReady 事件。
    • 在事件处理程序中,将彩色图像从 kinect 转换为 Bitmap。见here
    • 下载AForge 库并添加对“AForge.Video.FFMPEG”程序集的引用。
    • 使用VideoFileWriter 类将帧保存到视频文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 2012-08-23
      • 2017-12-01
      • 1970-01-01
      相关资源
      最近更新 更多