【问题标题】:BitmapSource to Bitmap Returns Null( EmguCV2.4.2.1777 + Kinect )BitmapSource 到 Bitmap 返回 Null(EmguCV2.4.2.1777 + Kinect)
【发布时间】:2013-01-05 04:11:51
【问题描述】:

嘿, 我正在尝试将 BitmapSource 从 Kinect RGB 颜色流转换为位图。我越来越空了。 我正在为 Windows SDK 1.6、Visual Studio 2012、Windows 7 Ultimate 64bit、EmguCV 2.4.2.1777 使用 Kinect。 这是代码:

void _kinect_ColorFrameReady( object sender, ColorImageFrameReadyEventArgs e )
    {
        using ( ColorImageFrame colorFrame = e.OpenColorImageFrame() )
        {
            if ( colorFrame == null )
            {
                return;
            }

            if ( colorFrame != null )
            {
                this.colorPixels = new byte[colorFrame.PixelDataLength];

                colorFrame.CopyPixelDataTo( this.colorPixels );

                int stride = colorFrame.Width * 4;

                colorBmp = BitmapSource.Create( 
                    colorFrame.Width, 
                    colorFrame.Height, 
                    96, 
                    96, 
                    PixelFormats.Bgr32, 
                    null, 
                    colorPixels,
                    stride 
                );

                currentColorFrame = new Image<Bgr, Byte>( colorBmp.ToBitmap() );

                this.imgOutput.Source = ImageHelpers.ToBitmapSource( currentColorFrame ); 
            }
        }           
    }

辅助方法:

   public static System.Drawing.Bitmap ToBitmap(this BitmapSource bitmapsource)
    {
        System.Drawing.Bitmap bitmap;
        using ( var outStream = new MemoryStream() )
        {
            // from System.Media.BitmapImage to System.Drawing.Bitmap
            BitmapEncoder enc = new BmpBitmapEncoder();
            enc.Frames.Add( BitmapFrame.Create( bitmapsource ) );
            enc.Save( outStream );
            bitmap = new System.Drawing.Bitmap( outStream );
            return bitmap;
        }         
    }


    [DllImport("gdi32")]
    private static extern int DeleteObject(IntPtr o);

    /// <summary>
    /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
    /// </summary>
    /// <param name="image">The Emgu CV Image</param>
    /// <returns>The equivalent BitmapSource</returns>
    public static BitmapSource ToBitmapSource(IImage image)
    {
        using (System.Drawing.Bitmap source = image.Bitmap)
        {
            IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                ptr,
                IntPtr.Zero,
                Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            DeleteObject(ptr); //release the HBitmap
            return bs;
        }
    }

请尽快指出我的错误或给我任何建议。

【问题讨论】:

    标签: c# kinect emgucv


    【解决方案1】:

    尝试如下更改 ToBitmap 方法中的 return 语句。 See also here.

    return (Bitmap)Image.FromStream(stream)
    

    请注意,ToBitmapSource 中的 CreateBitmapSourceFromHBitmap 可能会导致内存泄漏。 See this post。还有许多其他方法可以将 Bitmap 转换为 BitmapSource,但根据我的经验,没有一种方法更快。

    【讨论】:

      【解决方案2】:

      quick search 提供了一些关于如何将 BitmapSource 转换为 Bitmap 的资源。

      Is there a good way to convert between BitmapSource and Bitmap?

      http://snipplr.com/view/63090/how-to-convert-bitmapsource-to-bitmap/

      https://gist.github.com/916300

      它们都遵循一个总体主题,尽管略有不同。

      【讨论】:

      • 在来这里之前我已经完成了所有这些。 :) 我不知道,为什么我得到空值。我想,这是因为 EmguCV 2.4。在 emguCV 2.3 中它运行良好。
      • 这很奇怪。您是在使用 EmguCV 进行其他操作,还是仅用于此操作?
      • 也用于检测和识别。
      【解决方案3】:

      由于您使用的是 64 位操作系统,希望您也参考以下文件(对于 EMGU CV 版本 >=2.4)

      cudart64_42_9.dll, cvextern.dll, npp64_42_9.dll

      如果您不进行 64 位构建,这将有助于在 64 位操作系统中使用 emgucv。 除此之外,您的代码看起来还不错。

      【讨论】:

        猜你喜欢
        • 2020-01-28
        • 2016-06-15
        • 1970-01-01
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 2014-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多