将字节数组转换为Mat



我有一个CCD驱动程序,它会将IntPtr返回给我。我使用了Marshal。复制到字节数组(byterarray_Image(,字节数组_Image中的每个元素都存储8位R/G/B值,顺序为byte[0]=R值,byte[1]=G值,byte[2]=B值。。。等等。我已经成功地转换为3信道垫使用以下代码片段:

var src=new Mat(行:nHeight,列:nWidth,类型:MatType.CV_8UC3(;var索引器=src。GetGenericIndexer((;

int x = 0;
int y = 0;
for (int z = 0; z < (bytearray_Image.Length - 3); z += 3)
{
byte blue = bytearray_Image[(z + 2)];
byte green = bytearray_Image[(z + 1)];
byte red = bytearray_Image[(z + 0)];
Vec3b newValue = new Vec3b(blue, green, red);
indexer[y, x] = newValue;
x += 1;
if (x == nWidth)
{
x = 0;
y += 1;
}
}

由于图像非常大,因此这种方法似乎太慢,无法转换图像。有什么方法可以有效地进行这种转换吗?

此代码适用于我:

var image = new Mat(nHeight, nWidth, MatType.CV_8UC3);
int length = nHeight * nWidth * 3; // or image.Height * image.Step;
Marshal.Copy(bytearray_Image, 0, image.ImageData, length);

但只有当byte[]数据的步长等于Mat的时,它才会对您起作用

最新更新