Last active
August 29, 2015 14:20
-
-
Save hak8or/9129cd8ce5350eda9a0e to your computer and use it in GitHub Desktop.
Proper diff for motion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gab.opencv.*; | |
import processing.video.*; | |
import java.awt.*; | |
Capture webcam_frame; | |
OpenCV opencv; | |
// Capture resolution of webcam. | |
int capture_x = 960; | |
int capture_y = 720; | |
void setup() { | |
// Dump list of webcams capabilities. | |
get_webcam_capabilities(); | |
// Make viewport size. | |
size(capture_x * 2, capture_y); | |
// Resolution + fps requested from webcam. | |
webcam_frame = new Capture(this, capture_x, capture_y, 15); | |
// Opencv wrapper. | |
opencv = new OpenCV(this, capture_x, capture_y); | |
// Send the request for webcam to start dumping frames. | |
webcam_frame.start(); | |
// Disable looping in draw. | |
noLoop(); | |
} | |
// Dumps the webcam frames. | |
void draw() { | |
// Store the current frame. | |
opencv.loadImage(webcam_frame); | |
// Get the next frame. | |
webcam_frame.read(); | |
// Get only blue stuff; | |
opencv.getB(); | |
// Do a diff between the old and new image frames. | |
// opencv.diff(webcam_frame); | |
// Display the video image on the processing window. | |
set(0, 0, webcam_frame); | |
image(opencv.getSnapshot(), capture_x, 0); | |
} | |
// Unused since PImage doesn't seem to properly hold | |
// the old camera image. | |
PImage get_diff(PImage image_A, PImage image_B){ | |
opencv.loadImage(image_A); | |
opencv.diff(image_B); | |
return opencv.getSnapshot(); | |
} | |
// Dump list of webcams capabilities. | |
void get_webcam_capabilities(){ | |
// Read all camera params. | |
String[] cameras = Capture.list(); | |
// Dump all params to terminal. | |
println("Available cameras:"); | |
for (int i = 0; i < cameras.length; i++) | |
println(cameras[i]); | |
} | |
// Called everytime a new camera frame is avalible. | |
void captureEvent(Capture webcam) { | |
println("Capture event fired"); | |
// Redraw the GUI. | |
redraw(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment