OpenCV and Processing 11

In this example, I demonstrate the use of the image processing module (Imgproc) with the filter2D function. It is similar to the generic Photoshop filter where you can design the convolution matrix. The example references the OpenCV tutorial and uses a sharpen filter. It also uses the CVImage class in the last post.

Here is a screen shot of the Processing window.


import processing.video.*;
 
import org.opencv.imgproc.Imgproc;
import org.opencv.core.CvType;
 
Capture cap;
CVImage img;
PShape shp;
Mat kernel; // Convolution kernel
 
void setup() {
  size(640, 480, P3D);
  println(Core.VERSION);
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  cap = new Capture(this, width, height);
  cap.start();
  img = new CVImage(width, height);
  shp = createShape(RECT, 0, 0, width, height);
  kernel = new Mat(3, 3, CvType.CV_32FC1);
  kernel.put(0, 0, 0);
  kernel.put(0, 1, -1);
  kernel.put(0, 2, 0);
  kernel.put(1, 0, -1);
  kernel.put(1, 1, 5);
  kernel.put(1, 2, -1);
  kernel.put(2, 0, 0);
  kernel.put(2, 1, -1);
  kernel.put(2, 2, 0);
}
 
void draw() {
  background(0);
  arrayCopy(cap.pixels, img.pixels);
  img.toCV(); // load the pixels to Mat
 
  Mat src = img.getBGR();
  Mat dst = new Mat(src.size(), src.type());
  Imgproc.filter2D(src, dst, src.depth(), kernel);
 
  img.fromCV(dst); // update the img with Mat dst
  shp.setTexture(img);
  shape(shp, 0, 0);
  text("Frame rate: " + nf(round(frameRate), 2), 10, 20);
  src.release();
  dst.release();
}
 
void captureEvent(Capture c) {
  c.read();
  c.loadPixels();
}