Go Down

Topic: Processing JMyrom ColorTracker - cant find my error (Read 677 times) previous topic - next topic

moe69

Mar 10, 2012, 08:41 pm Last Edit: Mar 10, 2012, 08:48 pm by moe69 Reason: 1
hi there,

i got a problem with my color tracking code in processing.
the communication with arduino and the servo control is working, but dont find my error in one IF-case:

here is my .pde processing file:

(the line of my error if-case is marked with "//WHERE IS THE ERROR ??? BEGIN --------------")

Code: [Select]


/*
Object Tracker
*/
import processing.serial.*;
Serial port;
PFont font;




import JMyron.*;
JMyron m;
int z;
int c;
int xposition;
int rectsize;
int []b;
int []p;
int []a;
//int [][]a;
float cred;
float cgreen;
float cblue;
int credint;
int cgreenint;
int cblueint;



//SETUP--------------------------------------------------
void setup(){
 int w = 320;
 int h = 240;
 size(w,h);
 m = new JMyron();
 m.start(320,240);
 m.findGlobs(1);
 println("Myron " + m.version());
   
 font = createFont("Arial Bold",48);
   
 println(Serial.list()); // List COM-ports
 port = new Serial(this, Serial.list()[1], 57600);
 
}




//FunctionDRAW----------------------------------
void draw(){
 
 m.update();
 int[] img = m.image();
 
 //first draw the camera view onto the screen
 loadPixels();
 for(int i=0;i<width*height;i++)
     {
     pixels[i] = img[i];
     }
 updatePixels();
 
 
 
 
 //draw an averaged color block where the mouse is.
 noStroke();
 int c = m.average(mouseX-5,mouseY-5,mouseX+5,mouseY+5);
 fill(red(c),green(c),blue(c));
 credint = int(red(c));
 cgreenint = int(green(c));
 cblueint = int(blue(c));
 rect(mouseX-5,mouseY-5,10,10);
 
 // choose your color  
 m.trackColor(240,110,0,90); // code of tracked color !!!! last number = threshold
 
 
 
 noFill();  //boxes = hollow
 int[][] a; // initialize the DATA ARRAY a



 
 //draw center points of globs  !!! use a[0] to have the first point, which is center of biggest box
 a = m.globCenters();
 stroke(255,255,0);
 for(int i=0;i<a.length;i++)
 {
 int[] p = a[0];
 point(p[0],p[1]);
 }  
 
 
 
 
 //draw bounding boxes of globs  !!! use a[0] to have always the biggest box!
 a = m.globBoxes();
 stroke(255,0,0);
 //int z=0;
 for(int i=0;i<a.length;i++)
     {
     int[] b = a[0]; // rect values (x,y,width,heigth) - get biggest box!
     noFill();
     rect(b[0], b[1], b[2], b[3]);
     rectsize = b[2]*b[3];
     
     //WHERE IS THE ERROR ??? BEGIN --------------
 
     if(a.length  == 0) // if array size == 0
         {
         textFont(font,20);
         fill(255, 0, 255);
         text("no object",120,110);
         xposition = 160;
         }
      if(a.length >= 0) // if array size >= 0
         {
          textFont(font,12);
          fill(0, 255, 0);
          text("object locked",230,10);  
          xposition=b[0]+(b[2]/2);  
         }
      //WHERE IS THE ERROR ??? END-----------------------
     
     }

//write to serial port xposition to servo = arduino
port.write(xposition);

//read on serial port
int sens = port.read();

//text shown on video
textFont(font,12);
 fill(255, 0, 255);
 text("X video ",10,10);
 text(xposition,100,10);
 text("X send: ",10,20);
 text(xposition/2,100,20);
 text("Rectsize: ",10,30);
 text(rectsize,100,30);
 text("Size array a: ",10,40);
 text(a.length,100,40);
 text("Centerwidth: ",10,50);
 text(sens,100,50);
 text("R: ",10,60);
 text(credint,100,60);
 text("G: ",10,70);
 text(cgreenint,100,70);
 text("B: ",10,80);
 text(cblueint,100,80);

//lines shown on video
stroke(0,255,0);
line( (320/2)-sens, 0, (320/2)-sens, 240);
line( (320/2)+sens, 0, (320/2)+sens, 240);



}

//FunctionMOUSEPRESSED---------------------------------
//void mousePressed(){
//int state = 1;
//}  




in array a the data of globs is stored. if data a is empty or 0, there is no color/object on screen.

a.length == 0   ---> no object on screen
a.length >= 1 ---> object on screen

but it doesnt work, when
a.length >=1 ,it works
a.length == 0 ,doesnt work, and i dont know why

i also tried a.length == null  , that doesn work either....

dont know where the error is.
i declared a as an global variable
at the end of code i print a.length on screen.... and length 0 is printed, so the variable works
it seems there is something wrong with my if code

can somebody help me plz?

---moe


PaulS

Code: [Select]
      if(a.length  == 0) // if array size == 0
          {
          textFont(font,20);
          fill(255, 0, 255);
          text("no object",120,110);
          xposition = 160;
          }
       if(a.length >= 0) // if array size >= 0

So, when a.length IS 0, both blocks get executed. That hardly seems  like what you want.

moe69

hi,
thanks for replay

you are right. but i uploaded the wrong code :(

the code is like this, and it doesnt work:

Code: [Select]

if(a.length  <= 0) // if array a is empty, no object on screen
          {
          textFont(font,20);
          fill(255, 0, 255);
          text("no object",120,110);
          xposition = 160;
          }
       if(a.length >= 1) // if array a is not empty, object on screen
          {
           textFont(font,12);
           fill(0, 255, 0);
           text("object locked",230,10); 
           xposition=b[0]+(b[2]/2);   
          }


so, if a.length = 0 , there is no object
if a.length =1 or bigger, there is an object

where┬┤s the error?

---moe

PaulS

Quote
the code is like this, and it doesnt work:

"But it doesn't work" is nothing but whining.

That code does something. You want to see it doing something. If those two somethings were the same thing, you most likely would not be saying "but it doesn't work".

However, we have no idea what it is doing, or what it is you want/expect/need it to be doing.

moe69

#4
Mar 11, 2012, 05:43 pm Last Edit: Mar 11, 2012, 05:45 pm by moe69 Reason: 1
ok... i dont know how to explain.

i got an array A
this array A is generated when the tracked color is on screen(webcam)

if array A == empty, or A.length == 0            ---> print:" no color/object on screen"

if matrix A== not empty, or A.length >=1     ----> print:"color/object on screen"

those are the two cases for my print order

this is my code

Code: [Select]


 a = m.globBoxes();
 
 for(int i=0;i<a.length;i++)
     {
     int[] b = a[0]; // 0 to get biggest box only
     noFill();
     rect(b[0], b[1], b[2], b[3]);
     rectsize = b[2]*b[3];
     
     //WHERE IS THE ERROR ??? BEGIN --------------
 
     if(a.length  == 0) // if array size == 0
         {
         textFont(font,20);
         fill(255, 0, 255);
         text("no object on screen",120,110);
         xposition = 160;
         }
      if(a.length >= 1) // if array size >= 1
         {
          textFont(font,12);
          fill(0, 255, 0);
          text("object on screen",230,10);  
          xposition=b[0]+(b[2]/2);  
         }
      //WHERE IS THE ERROR ??? END-----------------------
     
     }




it always says, "object on screen", even when threre is no object on screen and array size == 0
i made a print order, to output the a.length on screen

even when print screen says a.length == 0, it doesnt says "no object on screen"

so, the big problem is, why is this if case ("no object on screen") not be called.

sorry for my bad english  :smiley-zipper:

---moe


PaulS

In your original code, you have a global one dimensional array named a and a local two dimensional array named a.

Why?

moe69

#6
Mar 12, 2012, 12:17 pm Last Edit: Mar 12, 2012, 12:30 pm by moe69 Reason: 1
yes thats right.
this code isnt mine. i modified it.

the array is a two dimensional array.
i deletet the one dimensional declaration.

this is what i want to try:

the function m.globBoxes()  gives my xposition, yposition, width and length of a box around a defined color.

if a screen color matches the tracked color, it looks like this:



if the color is not on screen, and the a.length of glob boxes is 0, it looks like this at the moment:



you see, the "object locked" at upper right corner is gone. but there is no "no object" text. also the xposition is not set to 160px like in the if-case

but you see on upper left, the array size a = 0!

------------

i also tried other cases. i used the rectsize. i multiplied the width and length of the box around the color.
u can see its value in the upper left. but the problem ist, when the color(object) is out of screen very fast it is not set to 0. when i move the object slowly far away and rectsize drops under a limit (ie. 800) it works like this:



you see now the if-case is working.

so the question is, why is it not working with the a.length command? even when it says a.length = 0

---moe

---moe

PaulS

Code: [Select]
  for(int i=0;i<a.length;i++)
Your test for a.length == 0 in this for loop is in the wrong place. The code will never be executed, if a.length is 0, because the whole for loop will be skipped.

moe69

#8
Mar 12, 2012, 07:55 pm Last Edit: Mar 12, 2012, 08:03 pm by moe69 Reason: 1
ha! you are absolutely right!

try to modify the code now!

thank you so much!
:)

---moe

EDIT:

i modified the code and it works! thank you!
and thank you for your patience! :)

great forum!!!

Go Up