Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Created May 19, 2015 20:35
Show Gist options
  • Save kasperkamperman/0605812c7fb35515ac6c to your computer and use it in GitHub Desktop.
Save kasperkamperman/0605812c7fb35515ac6c to your computer and use it in GitHub Desktop.
frameDifference with Mat OpenCV library for Processing
import org.opencv.core.Mat;
import gab.opencv.*;
import processing.video.*;
Capture video;
OpenCV opencv;
Mat cvLastFrameMat;
Mat cvThisFrameMat;
void setup()
{
size ( 640, 480 );
video = new Capture(this, width, height);
video.start();
opencv = new OpenCV( this, 640, 480); // Initialises the OpenCV library
// Any idea how to create an empty Mat?
// new Mat(640, 480, 0); gives an error however cols and rows seem correct
cvLastFrameMat = opencv.getGray();
//println(cvLastFrame.cols());
frameRate(25);
}
void draw()
{
if (video.available()) {
video.read();
opencv.loadImage(video);
cvThisFrameMat = opencv.getGray();
// difference with last Mat and this Mat
// result is stored in this Mat
OpenCV.diff(cvThisFrameMat, cvLastFrameMat);
image( opencv.getOutput(), 0, 0 );
// this gives a strange grid, so we do load image again
//OpenCV.toCv(video, cvLastFrameMat);
opencv.loadImage(video);
cvLastFrameMat = opencv.getGray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment