access an individual blob
in
Contributed Library Questions
•
1 year ago
Hey guys
im working with opencv, and have been working with code below.
what i cant work out how to do is access an individual blob to apply a function to it.
what I want to do is, for example colour blob[5] in the array red. can anyone help?
thanks in advance! code below...
import hypermedia.video.*;
import java.awt.*;
/*
Press SPACEBAR to record the background, ready to detect change.
Drag mouse inside sketch window whilst holding button down to change threshold!
*/
OpenCV opencv;
int w = 320; //camera image size, not too large else too many pixels so multiply in size()
int h = 240;
int threshold = 80;
boolean find=true;
//control variables:
final int maxNumOfBlobs = 20;
final int minBlobArea = 100; //area, in pixels
final int maxBlobArea = w*h/3;
void setup() {
size( w*2, h*2 );
opencv = new OpenCV( this );//opencv = main object for all computer vision processes.
opencv.capture(w, h);//Allocate and initialize resources for reading video stream
}
void draw() {
background(0);
opencv.read();
opencv.flip( OpenCV.FLIP_HORIZONTAL );
// image( opencv.image(), 10, 10 );
// RGB image
// image( opencv.image(OpenCV.GRAY), 20+w, 10 ); // GRAY image
// image( opencv.image(OpenCV.MEMORY), 10, 20+h ); // image in memory
opencv.absDiff();
opencv.threshold(threshold);
//image( opencv.image(OpenCV.GRAY), 0, 0, width, height); // absolute difference image
/* working with blobs
blobs(minArea, maxArea, maxBlobs, findHoles, maxVertices);
can omit (maxVertices)
*/
Blob[] blobs = opencv.blobs( minBlobArea, maxBlobArea, maxNumOfBlobs, true );
noFill();
pushMatrix();
//translate(0,20+h);
for ( int i=0; i<blobs.length; i++ ) {
Rectangle bounding_rect = blobs[i].rectangle;//????
float area = blobs[i].area;
float circumference = blobs[i].length;
Point centroid = blobs[i].centroid;//centre point
Point[] points = blobs[i].points;//holds the coordinates of the array of blobs
float blobX = bounding_rect.x*2;
float blobY = bounding_rect.y*2;
println( points[0].x );//how to acces blob coordinates
println(blobs[0]);
fill(255, 255, 255);
// stroke( blobs[i].isHole ? 128 : 64 );
//draw the blobs:
rect( blobX, blobY, bounding_rect.width, bounding_rect.height );
}//close for loop
popMatrix();
} //close draw
//for capturing background image, press space
void keyPressed() {
if ( key==' ' ) opencv.remember();
}
//for changing threshold
void mouseDragged() {
threshold = int( map(mouseX, 0, width, 0, 255) );
}
public void stop() {
opencv.stop();
super.stop();
}
1