OpenCV and Processing 13

In this example, we move on to the Video module of OpenCV 3.0.0. The first function we test is the Dense Optical Flow. It demonstrates the use of the calcOpticalFlowFarneback function http // downloaden. Again it makes use of the previous CVImage object to bridge between the Processing PImage and OpenCV Mat adobe photoshop cs2 nederlands gratisen. The example also reduces the size of the video (using the variable factor) before sending it for the optical flow processing; otherwise, the process can be lengthy herunterladen.

The optical flow process will basically compare two consecutive frames (the Mat last and grey) from the live webcam video. It will try to compute where the current pixels move to in the new frame albums kostenlosen.

Here are a number of screen shots from the sample run.

import org.opencv.core.CvType;
import org.opencv.core.Mat;
Capture cap;
CVImage img;
Mat last;
int w, h;
int factor;
void setup() {
  size(640, 480, P3D);
  factor = 8;
  w = width/factor;
  h = height/factor;
  cap = new Capture(this, width, height);
  img = new CVImage(w, h);
  last = new Mat(h, w, CvType.CV_8UC1);
void draw() {
  img.copy(cap, 0, 0, cap.width, cap.height, 0, 0, img.width, img.height);
  Mat grey = img.getGrey();
  Mat flow = new Mat(last.size(), CvType.CV_32FC2);
  Video.calcOpticalFlowFarneback(last, grey, flow, 0.5, 3, 10, 2, 7, 1.5, Video.OPTFLOW_FARNEBACK_GAUSSIAN);
  text("Frame rate: " + nf(round(frameRate), 2), 10, 20, 0);
void drawFlow(Mat _m) {
  for (int y=0; y<_m.rows (); y++) {
    for (int x=0; x<_m.cols (); x++) {
      double [] pt = _m.get(y, x);
      double dy = pt[1];
      double dx = pt[0];
      if (dx == 0 && dy == 0) 
      int x1 = x*factor;
      int y1 = y*factor;
      int x2 = round((x+(float)dx) * factor);
      int y2 = round((y+(float)dy) * factor);
      x1 = constrain(x1, 0, cap.width-1);
      y1 = constrain(y1, 0, cap.height-1);
      x2 = constrain(x2, 0, cap.width-1);
      y2 = constrain(y2, 0, cap.height-1);
      color col = cap.pixels[y1*cap.width+x1];
      line(x1, y1, 0, x2, y2, 0);
void captureEvent(Capture _c) {;

The technical reference for the algorithm is:

Gunnar Farnebäck. Two-frame motion estimation based on polynomial expansion. In Image Analysis, pages 363–370. Springer, 2003.