【问题标题】:Processing Kinect DepthFrame with Egmu使用 Egmu 处理 Kinect DepthFrame
【发布时间】:2014-05-07 05:00:25
【问题描述】:

我正在尝试使用以下代码使用 Egmu Open CV 库从 Kinect 深度图像中提取背景:

    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." 和 StackTrace:

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 中创建的。是什么导致了这个问题?

【问题讨论】:

    标签: c# wpf opencv kinect


    【解决方案1】:

    这个问题最初也困扰了我一段时间。 Format16bppGrayScale 实际上并没有完全由微软实现,因此它不起作用(此外,我认为它与 WinForms 相关联,而不是 WPF)。在我的情况下,我为解决它所做的工作是使用另一种格式,例如 Bgr565。如果您仍在尝试使用 Emgu 将其变灰,则质量不会完美,但它不仅仅是可用的。

    我实际上问了几乎相同的问题here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多