0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.