这是一个比较简洁的转换方法
/// <summary>
/// 此函数有缺陷,目前只能转换MatType.CV_8UC1
/// </summary>
/// <param name="srcbit"></param>
/// <returns></returns>
public static Mat BitmapToMat(IImage srcbit)
{
byte[] result = new byte[srcbit.ImageSize];
Marshal.Copy(srcbit.PixelDataPtr, result, 0, (int)srcbit.ImageSize);
switch (srcbit.PixelType)
{
case MvGvspPixelType.PixelType_Gvsp_Mono8:
return new Mat((int)srcbit.Height, (int)srcbit.Width, new MatType(MatType.CV_8UC1), result);
//此处省略其它格式数据的转换代码.
default:
Log.Warning("目前不支持的数据格式:{0} ", srcbit.PixelType);
break;
}
return null;
}
要点: srcbit.PixelDataPtr 是非托管指针, 无需锁定 就可调用 Marshal.Copy