Skip to content

Instantly share code, notes, and snippets.

@hak8or
Created April 27, 2015 04:37
Show Gist options
  • Save hak8or/4522851f57435dda0877 to your computer and use it in GitHub Desktop.
Save hak8or/4522851f57435dda0877 to your computer and use it in GitHub Desktop.
done with blob detection
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;
class Bounding_box {
public int x;
public int y;
public int _width;
public int _height;
int x_drawing_offset;
int y_drawing_offset;
Bounding_box(Contour contour){
Rectangle rectangle = contour.getBoundingBox();
this.x = rectangle.x;
this.y = rectangle.y;
this._width = rectangle.width;
this._height = rectangle.height;
x_drawing_offset = 0;
y_drawing_offset = 0;
}
Bounding_box(Rectangle rectangle){
this.x = rectangle.x;
this.y = rectangle.y;
this._width = rectangle.width;
this._height = rectangle.height;
x_drawing_offset = 0;
y_drawing_offset = 0;
}
Bounding_box(int x, int y, int _width, int b_height){
this.x = x;
this.y = y;
this._width = _width;
this._height = b_height;
x_drawing_offset = 0;
y_drawing_offset = 0;
}
Bounding_box(){
this.x = 0;
this.y = 0;
this._width = 0;
this._height = 0;
x_drawing_offset = 0;
y_drawing_offset = 0;
}
void make_drawing_offset(int x, int y){
this.x_drawing_offset = x;
this.y_drawing_offset = y;
}
void draw(){
rect(this.x + x_drawing_offset, this.y + y_drawing_offset, this._width, this._height);
}
}
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());
// Wipe background.
rect(capture_x, 0, capture_x + capture_x, height);
// Find the largest bounding box.
Bounding_box bounding_box = find_largest_bounding_box(opencv_rect.findContours());
// Add a drawing offset to it since we are drawing it in the right "panal".
bounding_box.make_drawing_offset(capture_x, 0);
// And draw it!
bounding_box.draw();
}
// Get the largest contour and draw it.
Bounding_box find_largest_bounding_box(ArrayList<Contour> contours){
println("found " + contours.size() + " contours");
Contour largest_contour;
// Used to hold the largest contour in terms of area.
if (contours.size() > 0)
largest_contour = contours.get(0);
else
return new Bounding_box();
// Find the largest contour.
for (Contour contour : contours) {
// 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;
}
return new Bounding_box(largest_contour.getBoundingBox());
}
// 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