Confused with ofxOpenCv and Blobs

Hello

I am doing this little project with Openframeworks on a Mac, but I don’t seem to quite get it. I am using the ofxOpenCv Addon and I am trying to make a group of 30 “rectangles” interact with the blobs… to be more accurate: to make them attach themselves to the contour.

So someone finally told me to loop through the next code that is found on the ofxCvBlob.h file:

ofNoFill();
ofSetColor(0x00FFFF);
ofBeginShape();
for (int i = 0; i < nPts; i++){
ofVertex(x + pts[i].x, y + pts[i].y);
}
ofEndShape(true);

So I did it like this, right there on the ofxCvBlob.h file:

for(int j = 0; j < 30; j++){
space = nPts/30;
ofRect(x + pts[j*space].x, y + pts[j*space].y,20,20);
}

The results are ok, not amazing though. But my real problem is that I need to do the same thing but directly through the TestApp.h file, but there are this “x” and “y” floats:

ofVertex(x + pts[i].x, y + pts[i].y);

These are created at the beginning of the void draw:

void draw(float x = 0, float y = 0){

But I don’t what they are for… and how do I call them through the TestApp.h file…

Can anyone help me please, it shouldn’t be anything difficult I believe, but I don’t seem to get it!
Thanks a lot!

the x and y are the offsets that you pass in when you call ofxCvContouFinder’s draw function. ie, you say:

myContourFinder.draw( 100,100,640,480);

x,y,w,h are passed in there. we pass in x,y or x,y,w,h because often times you draw the contour information on top of an image, and that image might be drawn at a position and scale, so this helps you match both.

for now, try removing the x and y from your code and everything will be based around 0,0.

take care,
zach

Hey Zach!

THanks for explaining what is the x and y for! I kind of thought it was that.

The problem stills remains though, I had taken the x and y before and everything worked fine, but when I tried to put that code on the code written in the testApp.cpp file, the whole thing freezes and everything stops working… to the point that I can’t close the aplication anymore until I turn the computer off manually.

¿Do you know why this happens? I thought it was because of the x and y, but maybe not?

Here is how I wrote it in the testApp.cpp file in the draw section:
(be carefull because it may freeze your computer too)


// or, instead we can draw each blob individually,
// this is how to get access to them:

for (int i = 0; i < contourFinder.nBlobs; i++){
contourFinder.blobs[i].draw(0,0);
}

//30 RECTANGLES IN A STRAIGHT LINE:

ofFill();
ofSetColor(255,0,0);

if(contourFinder.nBlobs == 0){
for(int j=1; j <= 30; j++){
ofRect(j*45,screenH/2,20,20);
}
}

// AND I WANT THEM TO STICK TO THE CONTOUR IF ONE BLOB APPEARS:
// THE blub. THING, IS HOW I LINK nPts and pts[i] TO THIS FILE:
// (THIS IS WHAT MAKES EVERYTHING FREEZE)

if(contourFinder.nBlobs == 1){
for(int k = 0; k < 30; k++){
espacio = blub.nPts/30;
ofRect(blub.pts[k*espacio].x, blub.pts[k*espacio].y,20,20);
}
}

Any sugestions???

I think you are missing something in your code –

  
if(contourFinder.nBlobs == 1){  
for(int k = 0; k < 30; k++){  
espacio = blub.nPts/30;  
ofRect(blub.pts[k*espacio].x, blub.pts[k*espacio].y,20,20);  
}  
}  

won’t compile as is, – what is “blub” ? blub = contourFinder.blobs[0] ?

can you post up the whole code somewhere (as a zip) ?

take care,
zach

Hey!

Here is the .zip with the whole code, thanks for helping!!

src.zip

ok “blub” is never set to anything at all. it’s defined in the h file, but you never set it equal to anything before you use it, thus a nasty crash.

try:

  
  
if(contourFinder.nBlobs == 1){  
		for(int k = 0; k < 30; k++){  
				ofSetColor(255,k*8,0);  
				espacio = contourFinder.blobs[0].nPts/30;  
				ofRect(contourFinder.blobs[0].pts[k*espacio].x, contourFinder.blobs[0].pts[k*espacio].y,20,20);  
		}  
	}  
  

instead – I think this is what you mean, no?

  • zach

Zach thanks a lot!!

Now I see how i have to do it and everthing works pretty good!! Thanks again!

Fernando