0
votes

I have a java program that needs to create an OpenCV 2D Mat of type 16UC1 from a 1D byte array (width and height are known). The size of the byte array is W*H*2 and each pixel should be constructed from two consecutive bytes from the array.

In C++ OpenCV this is somewhat trivial because Mat constructor can take a (void*) pointer to data, but can I do it in java OpenCV without nested loops and constructing every uint16 from two bytes?

1

1 Answers

0
votes

So, I ended up converting java byte[] to short[] via java.nio ByteBuffer and ShortBuffer.

byte v[] = {0,0, 1,0, -1,0,     0,1,    1,1,   -1,1,    0,-1,  1,-1,  -1,-1 };
short s[] = new short[v.length/2];
ByteBuffer.wrap(v).asShortBuffer().get(s);
Mat m = new Mat(3,3, CvType.CV_16UC1);
m.put(0, 0, s);

Still looking for an OpenCV-native solution, though.