0

I'm trying to extract background from Kinect Depth Image using Egmu Open CV library using this code:

    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;
    }

I want to display two videos streams: one with images from Kinect on DepthImage and processed frame with extracted background on second Image. When I'm trying to create

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

I' getting ArgumentException: "Parameter is not valid.", and 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()

This application is created in WPF. What is causing this problem?

jacbar
  • 526
  • 2
  • 6
  • 18

1 Answers1

0

This problem stumped me for a while too initially. Format16bppGrayScale wasn't actually completely implemented by Microsoft and consequently it doesn't work (furthermore, I think it's associated with WinForms and not WPF). What I've done to work around it in my case is to use another format such as Bgr565. The quality won't be perfect if you're still trying to gray it using Emgu, but it's more than usable.

I actually asked the almost identical question here.

Community
  • 1
  • 1
Gaessaki
  • 836
  • 1
  • 8
  • 16