OpenCVSharp4编写ImageSource和Mat相互转换

using OpenCvSharp;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public Mat ImageSourceToMat(ImageSource imageSource)
{
    BitmapSource bitmapSource = (BitmapSource)imageSource;         
    // 将Bitmap转换为Mat对象
    Mat mat = BitmapSourceConverter.ToMat(bitmapSource);   
    return mat;
}



using OpenCvSharp;
using System.Windows.Media;
using System.Windows.Media.Imaging;

private ImageSource MatToImageSource(Mat mat)
{
    // 将Mat对象转换为BitmapSource
    BitmapSource bitmapSource = BitmapSourceConverter.ToBitmapSource(mat);
    // 创建一个ImageSource对象
    ImageSource imageSource = Imaging.CreateBitmapSourceFromBitmap(bitmapSource);
    return imageSource;
}

你可能感兴趣的:(opencv,wpf)