【问题标题】:How do you convert a C# WritableBitmap to a EmguCV/OpenCV Mat?如何将 C# WriteableBitmap 转换为 Emgu CV/OpenCV Mat?
【发布时间】:2017-06-03 18:37:55
【问题描述】:

我有一个 WritableBitmap,我想将它转换为 EmguCV/OpenCV Mat。我该怎么做呢?我已经从在线代码中尝试了几种链式解决方案(WritableBitmap -> Bitmap -> Map),但我没有找到任何可行的方法。谢谢!

【问题讨论】:

  • 你看到this link 并尝试了吗?
  • @NejcGalof 是的,我看到了那个答案。但是,它是针对 C++ 而不是 C#,所以它不起作用。

标签: c# opencv emgucv mat writeablebitmap


【解决方案1】:

我觉得这很好用:

    public static Mat ToMat(BitmapSource source)
    {
        if (source.Format == PixelFormats.Bgra32)
        {
            Mat result = new Mat();
            result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4);
            source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step);
            return result;
        }
        else if (source.Format == PixelFormats.Bgr24)
        {
            Mat result = new Mat();
            result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 3);
            source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step);
            return result;
        }
        else if (source.Format == PixelFormats.Pbgra32)
        {
            Mat result = new Mat();
            result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4);
            source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step);
            return result;
        }
        else
        {
            throw new Exception(String.Format("Conversion from BitmapSource of format {0} is not supported.", source.Format));
        }
    }

道格

【讨论】:

    猜你喜欢
    • 2016-08-16
    • 2022-06-25
    • 2016-09-19
    • 2015-04-08
    • 2018-04-16
    • 2011-06-28
    • 1970-01-01
    • 2021-11-24
    • 2013-10-21
    相关资源
    最近更新 更多