The example converts a PImage (ARGB) to a Mat (BGR). It then randomly samples a pixel to compare their color values in the two formats. In order to transfer the integer array pixels[] from the PImage to the byte array of the CV_8UC4 matrix, it makes use of the ByteBuffer and IntBuffer.
// Conversion from PImage (ARGB) to Mat (BGR) import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.CvType; import java.nio.*; import java.util.List; PImage img; Mat mat; void setup() { size(640, 480); background(0); println(Core.VERSION); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); img = loadImage("sample01.jpg"); mat = new Mat(img.height, img.width, CvType.CV_8UC3); noLoop(); } void draw() { Mat tmp = new Mat(img.height, img.width, CvType.CV_8UC4); // temporary matrix byte [] bArray = new byte[img.width*img.height*4]; // byte array for transfer ByteBuffer bb = ByteBuffer.allocate(img.width*img.height*4); bb.asIntBuffer().put(img.pixels); bb.get(bArray); tmp.put(0, 0, bArray); // temporary lists for alignment of color channels ArrayList<Mat> ch1 = new ArrayList<Mat>(); ArrayList<Mat> ch2 = new ArrayList<Mat>(); Core.split(tmp, ch1); ch2.add(ch1.get(3)); ch2.add(ch1.get(2)); ch2.add(ch1.get(1)); Core.merge(ch2, mat); tmp.release(); ch1.clear(); ch2.clear(); image(img, 0, 0); // compare the color information from the PImage and Mat int x = floor(random(img.width)); int y = floor(random(img.height)); color c1 = img.get(x, y); double [] c2 = mat.get(y, x); println("PImage color: " + red(c1) + "," + green(c1) + "," + blue(c1)); println("Mat color: " + c2[2] + "," + c2[1] + "," + c2[0]); } |
The tmp matrix is the buffer to read in the pixel information from the PImage, img.
Mat tmp = new Mat(img.height, img.width, CvType.CV_8UC4); |
Through the byte array bArray, the pixel information is copied to the tmp matrix.
bb.asIntBuffer().put(img.pixels); bb.get(bArray); tmp.put(0, 0, bArray); |