Ask Your Question

Modifying pixels efficiently in Java

asked 2013-11-09 11:13:59 -0600

There are a series of topics covering how to efficiently modify pixels in Java:

These threads suggest copying the Mat image to a temporary array (byte[]), working with the array, and copying back the array to the image. But that has the additional cost of copying the image twice (img to array, array to img). That can impact the performance if a lot of processing steps are required, especially if we need to mix OpenCV functions and custom processing.

In the JavaCV wrapper, there is the possibility of accessing directly the image pixels using java.nio.ByteBuffers:!searchin/javacv/bytebuffer$20pixel/javacv/14EPaKP__cM/oDqDs2Hte_kJ

Would it be possible in OpenCV to create a ByteBuffer that points to the data of the image? I was considering something like this

    ByteBuffer mybb = ByteBuffer.wrap(new MatOfByte(myMat).toArray());

but I am afraid MatOfByte.toArray() will create a copy of the image anyway, instead of returning the original data.

ByteBuffer requires a byte[] for its constructor, so Mat.dataAddr() does not work.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2013-11-27 21:06:03 -0600

As of OpenCV 2.4.7, it is not possible to create a ByteBuffer that points to the data of a Mat.

I have created a feature request ( for this functionality.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-11-09 11:13:59 -0600

Seen: 635 times

Last updated: Nov 27 '13