版本:opencvsharp-4.5.3.20211228
我们以三通道图片为例:
1:Mat->byte[]
Mat mat = Cv2.ImRead(fullpath);
var bytes = new byte[mat.Total()*3];
Marshal.Copy(mat.Data, bytes, 0, bytes.Length);
2:byte[]->Mat
var src = new Mat(height, width, MatType.CV_8UC3);
int length = height * width * 3;
Marshal.Copy(data, 0, src.Data, length);
其中,宽高可以这样获取:
var height = mat.Rows;
var width = mat.Cols;
var channels = mat.Channels();
var mattype = mat.Type();
PS:如果是对于Bitmap的图片,推荐用官方的包里的方法去转换不会出错:OpenCvSharp.Extensions.dll 里面有Bitmap和Mat的互相转换
ref: https://github.com/shimat/opencvsharp/issues/173 https://wiki.onakasuita.org/pukiwiki/?OpenCvSharp.Mat
|