We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Making blobs attractors (Read 996 times)
Making blobs attractors
Feb 19th, 2010, 2:55pm
 
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);
}
Re: Making blobs attractors
Reply #1 - Feb 20th, 2010, 11:24am
 
it mostly depends on your program, but it absolutely feasible in many ways; ie. you declare a blob array inside the draw function scope:
Code:
Blob[] blobs = opencv.blobs( 200, w*h/3, 20, true ); 


if you declare blobs as an array list you can declare it global without knowing a priori its size.

also you could store the number of blobs and/or their positions inside globally declared int variables.
Re: Making blobs attractors
Reply #2 - Feb 22nd, 2010, 11:36am
 
Thank you so much for the advice. I will try to make it an array list.

Really thanks for the help Smiley
Page Index Toggle Pages: 1