C# Bitmap转Mat类型

	/// 
    /// bitmap 位图转为mat类型 
    /// 
    /// 
    /// 
    public Mat Bitmap2Mat(Bitmap bitmap)
    {
        MemoryStream s2_ms = null;
        Mat source = null;
        try
        {
            using (s2_ms = new MemoryStream())
            {
                bitmap.Save(s2_ms, ImageFormat.Bmp);
                source = Mat.FromStream(s2_ms, ImreadModes.AnyColor);
            }
        } catch (Exception e){
            log.Error(e.ToString());
        }
        finally
        {
            if (s2_ms != null)
            {
                s2_ms.Close();
                s2_ms = null;
            }
            GC.Collect();
        }
        return source;
    }

//第二种
Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(image); //bitmap转 mat
Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat); // mat 转 bitmap

你可能感兴趣的:(C#,Opencv)