上次讲了bitmap 与hobject 格式的转换。这次讲一下 Mat 与hobject 格式转化方法。这种功能应该比较少有人会用到。因我主要做深度学习工业应用,我常用的图像库是opencv, 而自动化AOI部门主要用halcon,为了对接方便,我就研究了 这个内容。
关键点是如何将Mat 转为byte[],由于Mat 是非连续存储的,无法直接将Mat 转为数组,所以我采用一行一行循环的方式,为了提高速度,行内我采用内存复制的方式。如果采用逐点循环的方式,速度会比较慢。
这个功能还是比较有用的,有次我看到opencv 群里面有个人到处询问如何将Mat 转为数组,但没人理他。
1.灰度图转换之Mat -> hobject
public void Mat2HObjectBpp8(Mat mat, out HObject image)
{
int ImageWidth = mat.Width;
int ImageHeight = mat.Height;
int channel = mat.Channels();
long size = ImageWidth * ImageHeight * channel;
int col_byte_num = ImageWidth * channel;
byte[] rgbValues = new byte[size];
//IntPtr imgptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(rgbValues.Length);
unsafe
{