Noble, J., (2009). Programming Interactivity. United States: O' Reilly Media
// the following statement imports the OpenCV libraryimport hypermedia.video.*;
OpenCV opencv;
// screen size
int w = 640;
int h = 480;
// This is the threshold of detection
int threshold = 80;
boolean find=true;
void setup() {
size( w*2+30, h*2+30 );
opencv = new OpenCV( this );
opencv.capture(w,h);
}
void draw() {
background(0);
// read image from camera
opencv.read();
image( opencv.image(), 10, 10 ); // RGB image
image( opencv.image(OpenCV.GRAY), 20+w, 10 ); // GRAY image
// here the difference betwee background and previous image is drawn to screen
opencv.absDiff();
opencv.threshold(threshold);
image( opencv.image(OpenCV.GRAY), 20+w, 20+h ); // absolute difference image
// the following code detects the blobs and interpreted as rectangles
Blob[] blobs = opencv.blobs( 100, w*h/3, 20, true );
noFill();
pushMatrix();
translate(20+w,20+h);
for( int i=0; i
Rectangle bounding = blobs[i].rectangle;
noFill();
rect( bounding.x, bounding.y, bounding.width, bounding.height );
float area = blobs[i].area;
float circumference = blobs[i].length;
Point centroid = blobs[i].centroid;
//Points[] points this is the points which define the blob
Point[] points = blobs[i].points;
// centroid. this is the binary center of blob
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 );
fill(255,0,255,64);
stroke(255,0,255);
if ( points.length>0 ) {
beginShape();
for( int j=0; j
vertex( points[j].x, points[j].y );
}
endShape(CLOSE);
}
}
popMatrix();
}
// the space bar will take a new background image for pixel comparison
void keyPressed() {
if ( key==' ' ) opencv.remember();
}
// dragging the mouse alters the threshold
void mouseDragged() {
threshold = int( map(mouseX,0,width,0,255) );
}
public void stop() {
opencv.stop();
super.stop();
}
No comments:
Post a Comment