Ask Your Question
2

make Mat from buffer byte

asked Mar 23 '13

pocahentez gravatar image

Hi,


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?

Preview: (hide)

2 answers

Sort by » oldest newest most voted
4

answered Mar 25 '13

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);
Preview: (hide)

Comments

1

@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 (Sep 12 '13)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 (Sep 14 '13)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 (Nov 13 '13)edit

Just created a feature request (http://code.opencv.org/issues/3405) and attached the result of my tests.

Jose Gómez gravatar imageJose Gómez (Nov 28 '13)edit
0

answered Mar 23 '13

wuling gravatar image
Preview: (hide)

Comments

Question Tools

Stats

Asked: Mar 23 '13

Seen: 16,446 times

Last updated: Mar 25 '13