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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.