OpenCV and Processing 15

The coming example will be the sparse optical flow. Before that, we first work on the 2D feature points tracking. The function goodFeaturesToTrack() belongs to the Imgproc module. It takes in a greyscale image and identifies the feature points (corners) as a matrix of point, MatOfPoint. The sample code here uses the feature points to render a live graphics of the webcam image.


import processing.video.*;
 
import org.opencv.video.Video;
import org.opencv.core.CvType;
import org.opencv.core.TermCriteria;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
 
final int MAX_COUNT = 400;
Capture cap;
CVImage img;
TermCriteria term;
Size subPixWinSize, winSize;
 
void setup() {
  size(640, 480, P3D);
  background(0);
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  cap = new Capture(this, width, height);
  cap.start();
  cap.read();
  img = new CVImage(cap.width, cap.height);
  term = new TermCriteria(TermCriteria.COUNT | TermCriteria.EPS, 20, 0.03);
  subPixWinSize = new Size(10, 10);
  winSize = new Size(31, 31);
  noStroke();
  smooth();
}
 
void draw() {
  img.copy(cap, 0, 0, cap.width, cap.height, 0, 0, img.width, img.height);
  img.toCV();
  background(0);
 
  Mat grey = img.getGrey();
  MatOfPoint corners = new MatOfPoint();
  Imgproc.goodFeaturesToTrack(grey, corners, MAX_COUNT, 0.01, 10, new Mat(), 3, false, 0.04);
  if (corners.toArray().length>0) {
    MatOfPoint2f points = new MatOfPoint2f(corners.toArray());
    Imgproc.cornerSubPix(grey, points, subPixWinSize, new Size(-1, -1), term);
    if (points.toArray().length>0) {
      for (Point p : points.toArray()) {
        int x = constrain((int)p.x, 0, cap.width-1);
        int y = constrain((int)p.y, 0, cap.height-1);
        color col = cap.pixels[y*cap.width+x];
        fill(red(col), green(col), blue(col), random(100, 220));
        float s = random(5, 40);
        ellipse(x, y, s, s);
      }
    }
  }
 
  fill(255);
  text("Frame rate: " + nf(round(frameRate), 2), 10, 20, 0);
}
 
void captureEvent(Capture _c) {
  _c.read();
}