OpenCV and Processing 3

This example will demonstrate the use of CvType, Size and Scalar.

CvType defines all the matrix types in OpenCV. It describes the number of channels, and depth information for each element (pixel) in the matrix. This example will use CV_8UC1 – 8 bits unsigned char, 1 channel. To communicate with Processing, we usually take CV_8UC4 for ARGB as the RGB format in PImage also uses 4 bytes for storage.
 

Size defines the size or dimension of a matrix. It only has the width and height  properties. It is similar to the Java Dimension class.

Scalar is actually vector. It has the maximum of 4 dimensions. The example will use it to initialize values in a matrix.
 

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Size;
import org.opencv.core.Scalar;
 
void setup() {
  size(640, 480);
  println(Core.VERSION);
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  noLoop();
}
 
void draw() {
  background(0);
  Mat m = new Mat(new Size(4, 3), CvType.CV_8UC1, Scalar.all(255));
  println(m.dump());
}

 
The major line will create a new matrix of 4 columns by 3 rows with single channel. All elements will be initialized to 255.

Mat m = new Mat(new Size(4, 3), CvType.CV_8UC1, Scalar.all(255));

 
The result is

[255, 255, 255, 255;
 255, 255, 255, 255;
 255, 255, 255, 255]