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 --------------")
/*
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