I have a CCD driver which returns IntPtr to me. I used Marshal.Copy to byte array (bytearray_Image), each element inside bytearray_Image stores 8bit R/G/B value which the sequence is byte[0] = R value, byte[1] = G value, byte[2] = B value...and so on. I have successfully converted to 3 Channels Mat using below code snippet:
var src = new Mat(rows: nHeight, cols: nWidth, type: MatType.CV_8UC3);
var indexer = 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;
}
}
Since the image is very large, this method seems to be too slow to convert the image. Is there any ways to do such conversion efficiently?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…