give name (or number) to Blobs
in
Contributed Library Questions
•
1 year ago
Dear all interested readers,
I am making a blob detection sketch using the wonderfull OPENCV library. What I want now, is to label each blob with a number. How can I add this label?
This is the code I have now
I am making a blob detection sketch using the wonderfull OPENCV library. What I want now, is to label each blob with a number. How can I add this label?
This is the code I have now
- import hypermedia.video.*;
import java.awt.*;
import processing.video.*;
int counter;
Capture myCapture;
OpenCV opencv;
String[] captureDevices;
int w = 320;
int h = 240;
int threshold = 80;
boolean find=true;
PFont font;
void setup() {
size(w*3, h*3 );
opencv = new OpenCV( this );
opencv.capture(width, height);
println (Capture.list());
captureDevices = Capture.list();
myCapture = new Capture(this, width, height, 30);
font = loadFont( "AndaleMono.vlw" );
textFont( font );
opencv = new OpenCV(this);
opencv.allocate(width, height);
//opencv.cascade( OpenCV.CASCADE_FRONTALFACE_ALT );
}
void draw() {
background(0);
if (myCapture.available()) {
myCapture.read();
opencv.copy(myCapture);
image(opencv.image(), 0, 0, width, height);
opencv.absDiff();
opencv.threshold(threshold);
Blob[] blobs = opencv.blobs( 100, w*h, 20, true );
noFill();
pushMatrix();
for ( int i=0; i<blobs.length; i++ ) {
if (i > blobs.length) { //When "i" is less than "35"...
counter = 0;
}
Rectangle bounding_rect = blobs[i].rectangle;
float area = blobs[i].area;
float circumference = blobs[i].length;
Point centroid = blobs[i].centroid;
Point[] points = blobs[i].points;
noFill();
// stroke( blobs[i].isHole ? 128 : 64 );
rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );
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(40, 300, 20);
rect(centroid.x, centroid.y, bounding_rect.width, bounding_rect.height);
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 );
fill(255, 0, 0);
text( counter, centroid.x+5, centroid.y+15 );
fill(255, 255, 255);
}
popMatrix();
}
}
void keyPressed() {
if ( key==' ' ) opencv.remember();
}
void mouseDragged() {
threshold = int( map(mouseX, 0, width, 0, 255) );
}
public void stop() {
opencv.stop();
super.stop();
}
1