OpenCV and Processing 6

To perform the same image loading task in OpenCV, we use the imread() function in the Imgcodecs module. The function was in the Highgui module before. The image loaded in the Mat data structure is in BGR format. We shall use the split and merge commands to align the proper channels. Finally, we use a byte array to transfer the pixels information to the pixels[] array of a PImage object.

The codes are,

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import java.nio.*;
import java.util.List;
PImage img;
Mat mat;
Mat alpha;
void setup() {
  size(640, 480);
void draw() {
  mat = Imgcodecs.imread(dataPath("sample01.jpg"));
  Mat out = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC4);
  alpha = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC1, Scalar.all(255));
  byte [] bArray = new byte[mat.rows()*mat.cols()*4];
  img = createImage(mat.cols(), mat.rows(), ARGB);
  ArrayList ch1 = new ArrayList();
  ArrayList ch2 = new ArrayList();
  Core.split(mat, ch1);
  Core.merge(ch2, out);
  out.get(0, 0, bArray);
  image(img, 0, 0);
mat = Imgcodecs.imread(dataPath("sample01.jpg"));

The line will import the external file into a Mat object named mat. Another Mat object, out was defined to contain the final ARGB image. To create the alpha channel, we also need to define another single channel Mat object, alpha with all elements initialized to 255.

alpha = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC1, Scalar.all(255));

As we can only use byte array in this situation to transfer data to and from the Mat object, we create an empty byte array, bArray. The object img of type PImage will be used for display purpose.

To realign the channels from BGR to ARGB, we use the split() and merge() functions. Before that, we split the original mat image into 3 separate channels in ch1. The realigned 4 channels will be in the ch2 object and merged back to the out Mat object. The object out uses the get() function to transfer its data to the byte array, bArray. And finally, it is transferred to the pixels[] array of the img object.