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

最佳答案

我发现这很好用:

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

道格

关于c# - 如何将C#WriteableBitmap转换为Emgu CV/OpenCV Mat?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41735425/

10-11 16:50