Skip to content

Instantly share code, notes, and snippets.

@vinibaggio
Created April 17, 2011 20:05
Show Gist options
  • Save vinibaggio/924417 to your computer and use it in GitHub Desktop.
Save vinibaggio/924417 to your computer and use it in GitHub Desktop.
Código processing do básico
import hypermedia.video.Blob;
import java.awt.Rectangle;
public class BlobSizeComparator implements java.util.Comparator<Blob> {
public int compare(Blob a, Blob b) {
Rectangle boundingA = a.rectangle;
Rectangle boundingB = b.rectangle;
double areaA = boundingA.getHeight() * boundingA.getWidth();
double areaB = boundingB.getHeight() * boundingB.getWidth();
return (int)Math.round(areaB - areaA);
}
}
import hypermedia.video.*;
import java.awt.*;
OpenCV opencv;
PFont font;
int h = 640;
int w = 480;
int threshold = 0;
Blob
ArrayList<Blob> filterBlobs(Blob[] blobs, int numberOfBlobs) {
ArrayList<Blob> sortedBlobs = new ArrayList<Blob>(Arrays.asList(blobs));
BlobSizeComparator blobSizeComparator = new BlobSizeComparator();
Collections.sort(sortedBlobs, (Comparator<Blob>)blobSizeComparator);
for (int i = sortedBlobs.size() - 1; i >= numberOfBlobs; i--){
sortedBlobs.remove(i);
}
return sortedBlobs;
}
void showBlobs(ArrayList<Blob> blobs) {
noFill();
for(Blob blob: blobs) {
Rectangle bounding_rect = blob.rectangle;
float area = blob.area;
float circumference = blob.length;
Point centroid = blob.centroid;
Point[] points = blob.points;
// rectangle
noFill();
stroke( blob.isHole ? 128 : 64 );
rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );
// centroid
stroke(0,0,255);
line( centroid.x-5, centroid.y, centroid.x+5, centroid.y );
line( centroid.x, centroid.y-5, centroid.x, centroid.y+5 );
noStroke();
fill(0,0,255);
text( area,centroid.x+5, centroid.y+5 );
fill(255,0,255,64);
stroke(255,0,255);
if ( points.length>0 ) {
beginShape();
for( int j=0; j<points.length; j++ ) {
vertex( points[j].x, points[j].y );
}
endShape(CLOSE);
}
noStroke();
fill(255,0,255);
text( circumference, centroid.x+5, centroid.y+15 );
}
}
void showThreshold(int threshold) {
text("Current threshold: " + threshold, 10, height - 10);
}
void setup() {
size(h, w);
opencv = new OpenCV(this);
opencv.capture(h, w);
font = loadFont("AndaleMono.vlw");
textFont(font);
}
void draw() {
background(0);
opencv.read();
//opencv.flip(OpenCV.FLIP_HORIZONTAL);
opencv.absDiff();
opencv.blur(OpenCV.BLUR, 50); // Muito blur para evitar blobs de pequenos dedos, p.e.
opencv.threshold(threshold); // Grayscale
image(opencv.image(OpenCV.GRAY), 0, 0);
opencv.remember();
ArrayList<Blob> blobs = filterBlobs(opencv.blobs( 300, w*h/2, 20, false ), 1);
showBlobs(blobs);
showThreshold(threshold);
}
void mouseClicked() {
if (mouseButton == LEFT){
threshold += 10;
} else {
threshold -= 10;
}
}
public void stop() {
opencv.stop();
super.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment