Skip to content

Instantly share code, notes, and snippets.

@hak8or
Created April 27, 2015 03:04
Show Gist options
  • Save hak8or/aa2cc542e3a494dbdd30 to your computer and use it in GitHub Desktop.
Save hak8or/aa2cc542e3a494dbdd30 to your computer and use it in GitHub Desktop.
Rough blob detection working
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
Capture webcam_frame;
OpenCV opencv_blue;
OpenCV opencv2;
OpenCV opencv_diff;
OpenCV opencv_rect;
// Holds the previous processed frame.
PImage prev_frame;
// 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, 5);
// Opencv wrapper.
opencv_blue = new OpenCV(this, capture_x, capture_y);
opencv_blue.useColor(HSB);
opencv2 = new OpenCV(this, capture_x, capture_y);
opencv_diff = new OpenCV(this, capture_x, capture_y);
opencv_rect = new OpenCV(this, capture_x, capture_y);
opencv_diff.useGray();
// Send the request for webcam to start dumping frames.
webcam_frame.start();
// Disable looping in draw.
noLoop();
}
// Dumps the webcam frames.
void draw() {
// Get the next frame.
webcam_frame.read();
// Display the video image on the processing window.
// set(0, 0, webcam_frame);
// Store the current frame.
opencv_blue.loadImage(webcam_frame);
// Get only blue stuff;
opencv_blue.getB();
// Load that again but as input into the grayscale processor.
opencv2.loadImage(opencv_blue.getSnapshot());
opencv2.useGray();
opencv2.threshold(200);
opencv_rect.loadImage(opencv2.getSnapshot());
ArrayList<Contour> contours = opencv_rect.findContours();
println("found " + contours.size() + " contours");
// Do a diff between the old and new image frames.
// opencv_diff.diff(opencv2.getSnapshot());
// Display the processed image to the window.
// image(opencv2.getSnapshot(), capture_x, 0);
// Wipe background.
rect(capture_x, 0, capture_x + capture_x, height);
// Used to hold the largest contour in terms of area.
Contour largest_contour = new Contour();
// Draw all the contours
for (Contour contour : contours) {
// stroke(0, 255, 0);
// contour.draw();
Rectangle r = contour.getBoundingBox();
if (r.width * r.height > 20)
rect(r.x + capture_x, r.y, r.width, r.height);
else
continue;
// If the current contour is bigger than the one so far,
// make that one be the largest contour.
if (contour.area() > largest_contour.area())
largest_contour = contour;
}
}
// Called everytime a new camera frame is avalible.
void captureEvent(Capture webcam) {
println("Capture event fired");
// Redraw the GUI.
redraw();
}
// 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]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment