Hi,
I am using a simple particle and attractor sketch with blob detection.
The problem is that the open cv library is such that the blobs.length array number is only within the draw function and so I cannot get at it from anywhere else in my program.
Therefore as I want to create 1 attractor for every blob I have to declare attractors[i] under the same if statement as the blobs.length inside void draw. But then the problem remains how to access how many attractors there are outside void draw.
For example in my particles class I refer to the attractors[i] to create the movement of the particles.
So I just get a null pointer exception as the class can't see how many attractors there are. I must be doing something wrong (and I can't imagine the only way around it is to put everything inside the draw function?)
Here is the code in the void draw function just to show the issue in the code example provided for open cv - Is there a way anyone knows of making the blobs number and properties (eg x and y location) global?
Thank you.
Code:void draw()
{
// background(0.2, 0.1, 0.14);
///////
opencv.read();
//opencv.flip( OpenCV.FLIP_HORIZONTAL );
// opencv.convert( GRAY );
// 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), 20+w, 20+h ); // absolute difference image
// working with blobs
Blob[] blobs = opencv.blobs( 200, w*h/3, 20, true );
noFill();
pushMatrix();
//translate(20+w,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;
Point[] points = blobs[i].points;
// rectangle
noFill();
stroke( blobs[i].isHole ? 128 : 64 );
rect( bounding_rect.x, bounding_rect.y, bounding_rect.width, bounding_rect.height );
//println(blobs[0].centroid.x);
//Rect = new RECT (rectangle(blobs[i].centroid.x, blobs[i].centroid.y, 60, 60));
fill(255);
if (i>= 0){
attractor = new Attractor[blobs.length];
// attractor = new Attractor[blobs.length];
//rect(blobs[i].centroid.x, blobs[i].centroid.y, 20, 20);
attractor[i] = new Attractor(blobs[i].centroid.x,blobs[i].centroid.y);
//Rect = new RECT(
}
else{
attractor[0] = new Attractor(50,50);
}