//引用 OpenCvSharp4.Extensions
public static Bitmap ToBitmap(BitmapSource m)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data);
return bmp;
}
//Bitmap转BitmapSource
Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(showImg);
public static BitmapSource GetImageSouce(Bitmap bitmap)
{
BitmapSource img;
IntPtr hBitmap;
hBitmap = bitmap.GetHbitmap();
img = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return img;
}
//paddleocr中需要传入3通道或1通道的mat,此时要用CV2.cvtColor进行转换
src = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//bitmap转mat
Cv2.CvtColor(src, src, ColorConversionCodes.RGBA2RGB);//mat转三通道mat