Ask Your Question

make Mat from buffer byte

asked 2013-03-23 11:55:03 -0600

pocahentez gravatar image


I have a Byte buffer and i want to make a Mat.How can i achieve it using a java code?Should i make a bitmap with the byte buffer first then transform it to Mat?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-03-25 03:04:04 -0600

Andrey Pavlenko gravatar image

You need to allocate a Mat of the appropriate size and type and then use Mat.put() method:

byte buf[] = new byte[100];
Mat m = new Mat(1, 100, CvType.CV_8UC1);
m.put(0, 0, buf);
edit flag offensive delete link more



@AndreyPavlenko Is there a way to create a Mat backed by a NIO ByteBuffer allocated by us. Alternatively, is there a way to get the Buffer of a Mat ?

TimeManx gravatar imageTimeManx ( 2013-09-12 11:10:05 -0600 )edit

@TimeManx No, it's not possible: Mat reallocates its buffer on native level when needed, so it's hard to share it between native and java without copying...

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-09-14 10:27:17 -0600 )edit

In JavaCV's OpenCV wrapper, the IplImage has a "getDirectByteBuffer" native method that will from JNI create a new ByteBuffer with the address of the data of the Mat. In my tests modifying pixels like that performs much better than copying byte arrays to and from the Mat.

Jose Gómez gravatar imageJose Gómez ( 2013-11-13 17:37:52 -0600 )edit

Just created a feature request ( and attached the result of my tests.

Jose Gómez gravatar imageJose Gómez ( 2013-11-27 21:03:41 -0600 )edit

answered 2013-03-23 12:01:17 -0600

wuling gravatar image
edit flag offensive delete link more


Question Tools


Asked: 2013-03-23 11:55:03 -0600

Seen: 16,133 times

Last updated: Mar 25 '13