如何从Kinect录制RGB视频?

时间:2014-03-04 09:48:00

标签: c# kinect rgb

我正在尝试从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:

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

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