2014-05-01 50 views
0

我試圖使用Egmu打開簡歷庫使用此代碼提取Kinect的深度圖像背景:處理Kinect的DepthFrame與Egmu

private short[] _depthPixels; 
    private void OnDepthStreamReady(object sender, DepthImageFrameReadyEventArgs e) 
    { 
     using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame()) 
     { 
      if (depthImageFrame == null) return; 

      depthImageFrame.CopyPixelDataTo(_depthPixels); 
      var displaySource = BitmapSource.Create(
           640, 
           480, 
           96, 96, 
           PixelFormats.Gray16, 
           null, 
           _depthPixels, 
           640 * depthImageFrame.BytesPerPixel); 

      DepthImage.Source = displaySource; 


      var bitmap = ToBitmap(_depthPixels, 640, 480, PixelFormat.Format16bppGrayScale); 

      var emguImage = new Image<Gray, short>(bitmap); 

     } 
    } 

    private Bitmap ToBitmap(short[] pixels, int width, int height, PixelFormat format) 
    { 
     if (pixels == null) 
      return null; 

     var bitmap = new Bitmap(width, height, format); 

     var data = bitmap.LockBits(
      new Rectangle(0, 0, bitmap.Width, bitmap.Height), 
      ImageLockMode.ReadWrite, 
      bitmap.PixelFormat); 

     Marshal.Copy(pixels, 0, data.Scan0, pixels.Length); 

     bitmap.UnlockBits(data); 

     return bitmap; 
    } 

我想顯示兩個視頻流:一個與DepthImage從Kinect的圖像並在第二張圖像上提取背景進行處理。當我試圖創建

var emguImage = new Image<Gray, short>(bitmap); 

我越來越ArgumentException: "Parameter is not valid.",和堆棧跟蹤:

at System.Drawing.Bitmap.GetPixel(Int32 x, Int32 y) 
at Emgu.CV.Image`2.set_Bitmap(Bitmap value) 
at Emgu.CV.Image`2..ctor(Bitmap bmp) 
at Application.MainWindow.OnDepthStreamReady(Object sender, DepthImageFrameReadyEventArgs e) in e:\private\Application\MainWindow.xaml.cs:line 83 
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Windows.Threading.DispatcherOperation.Invoke() 
at System.Windows.Threading.Dispatcher.ProcessQueue() 
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
at System.Windows.Application.RunInternal(Window window) 
at System.Windows.Application.Run() 
at Application.App.Main() in e:\private\Application\obj\Debug\App.g.cs:line 0 
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Threading.ThreadHelper.ThreadStart() 

該應用程序在WPF創建。什麼導致了這個問題?

回答

0

這個問題最初也讓我難住了一段時間。 Format16bppGrayScale實際上並未完全由Microsoft實現,因此它不起作用(此外,我認爲它與WinForms而不是WPF關聯)。在我的例子中,我所做的解決它的方法是使用另一種格式,例如Bgr565。如果您仍然試圖使用Emgu對其進行灰化處理,質量將不盡如人意,但它不僅可用。

我其實問了幾乎相同的問題here