2014-03-04 93 views
1

我試圖從Kinect中捕獲RBG視頻。如何記錄Kinect的RGB視頻?

我已經在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) 
    { 
    } 
} 

如何修改我在光盤上寫入視頻文件的代碼?


Thaks爲您的答覆,我已經這樣做。

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

嘗試以下操作:

  • 訂閱來自傳感器的ColorFrameReady事件。
  • 在事件處理程序中將彩色圖像從kinect轉換爲Bitmap。見here
  • 下載AForge庫並添加對「AForge.Video.FFMPEG」程序集的引用。
  • 使用VideoFileWriter類將幀保存到視頻文件。