In OpenCV, the fastest way to work with a Mat is to first copy it to an array of the appropriate primitive type, something like this:
byte[][] array = new byte[rows][cols];
for (int i = 0; i < rows; i++) {
mat.get(i, 0, array[i]);
}
However, this doesn’t work in OpenCV4Android with unsigned Mat types (e.g. 8U), since Java lacks unsigned types. I could just copy to an array of the next larger primitive type (here, a short) while adding 256 to every element:
byte[] buf = new byte[cols];
short[][] array = new short[rows][cols];
for (int i = 0; i < rows; i++) {
mat.get(i, 0, buf);
for (int j = 0; j < cols; j++) {
array[i][j] = (short) (buf[i]+256);
}
}
Is there a faster way?
It’s actually possible to convert the Mat to signed byte before extracting it: