atduskgreg/opencv-processing — pull request #64 by mudphone (2014-10-07 08:41:25+00:00)
Title: Use ByteBuffer to convert from OpenCV Mat to PImage pixels array.

I have noticed quite a good performance boost when I remove the for-loop in OpenCV#toPImage(). The use of ByteBuffers allows for bulk copying of the pixel data, but requires "ordering" of the bytes before converting to an int array (when channels > 1).

I am a n00b with Processing PImage pixel data, so I'm not sure I have been able to comprehensively test these changes. I did my best and have put some test sketches here: https://github.com/mudphone/opencv-processing/tree/to-pimage-with-buffers-test-sketches/examples/TESTS

There is a sketch for the single (gray), 3 (RGB), and 4 (RGBA) channel cases. I notices a definite FPS increase when using this technique on machines with lower-powered CPUs. Even on a newer MBPro, I was able to see some improvement in the video sketches.

Please let me know if there are any other cases you'd like me to test. Or, if I'm just plain missing some important use cases. I have only really used this for a single production sketch.

Credit for the idea to use ByteBuffer comes from Bryan Chung's blog: http://www.magicandlove.com/blog/2014/02/27/conversion-between-processing-pimage-and-opencv-cvmat/