if statement - Showing an image and replacing it with another image if something happens -
i use color tracking code processing.
what want (example):
- if red detected show image 1
- if green detected show image 2
- if blue detected show image 3
the problem is, if last color detected , last image shown, , track first color first image not in front (i can't see it).
the whole code:
import processing.video.*; //import hypermedia.net.*; pimage img; pimage img2; pimage img3; capture video; final int tolerance = 20; float xrc = 0;// xy coordinate of center of first target float yrc = 0; float xrh = 0;// xy coordinate of center of second target float yrh = 0; float xrc2 = 0; // xy coordinate of center of third target float yrc2 = 0; float xrh2 = 0;// xy coordinate of center of fourth target float yrh2 = 0; int ii=0; //mouse click counter color trackcolor; //the first color center of robot color trackcolor2; //the second color head of robot color trackcolor3; //the first color center of robot 2 color trackcolor4; //the first color center of robot 2 void setup() { img = loadimage("img_4700.jpg"); img2 = loadimage("2.jpg"); img3 = loadimage("3.jpg"); size(800,800); video = new capture(this,640,480); video.start(); trackcolor = color(94,164,126); trackcolor2 = color(60,110,194); trackcolor3 = color(197, 76,64); trackcolor4 = color(255,0,0); smooth(); } void draw() { background(0); if (video.available()) { video.read(); } video.loadpixels(); image(video,0,0); float r2 = red(trackcolor); float g2 = green(trackcolor); float b2 = blue(trackcolor); float r3 = red(trackcolor2); float g3 = green(trackcolor2); float b3 = blue(trackcolor2); float r4 = red(trackcolor3); float g4 = green(trackcolor3); float b4 = blue(trackcolor3); float r5 = red(trackcolor4); float g5 = green(trackcolor4); float b5 = blue(trackcolor4); int somme_x = 0, somme_y = 0; // pour le calcul des baricentres int compteur = 0; int somme_x2 = 0, somme_y2 = 0; // pour le calcul des baricentres int compteur2 = 0; int somme_x3 = 0, somme_y3 = 0; // pour le calcul des baricentres int compteur3 = 0; int somme_x4 = 0, somme_y4 = 0; // pour le calcul des baricentres int compteur4 = 0; for(int x = 0; x < video.width; x++) { for(int y = 0; y < video.height; y++) { int currentloc = x + y*video.width; color currentcolor = video.pixels[currentloc]; float r1 = red(currentcolor); float g1 = green(currentcolor); float b1 = blue(currentcolor); if(dist(r1,g1,b1,r2,g2,b2) < tolerance) { somme_x += x; somme_y += y; compteur++; } else if(compteur > 0) { xrc = somme_x / compteur; yrc = somme_y / compteur; } if(dist(r1,g1,b1,r3,g3,b3) < tolerance) { somme_x2 += x; somme_y2 += y; compteur2++; } else if(compteur2 > 0) { xrh = somme_x2 / compteur2; yrh = somme_y2 / compteur2; } if(dist(r1,g1,b1,r4,g4,b4) < tolerance) { somme_x3 += x; somme_y3 += y; compteur3++; } else if(compteur3 > 0) { xrc2 = somme_x3 / compteur3; yrc2 = somme_y3 / compteur3; } if(dist(r1,g1,b1,r5,g5,b5) < tolerance) { somme_x4 += x; somme_y4 += y; compteur4++; } else if(compteur4 > 0) { xrh2 = somme_x4 / compteur4; yrh2 = somme_y4 / compteur4; } } } // track color , show images boolean c1 = false; boolean c2 = false; boolean c3 = false; if(xrc != 0 || yrc != 0) { // color green detected c1 = true; c2 = false; c3 = false; } if(xrh != 0 || yrh != 0) { // color blue detected c2 = true; c1 = false; c3 = false; } if(xrc2 != 0 || yrc2 != 0) { // color red detected c3 = true; c1 = false; c2 = false; } if(c1 == true) { image(img,0,0); // show image 1 } else if (c2 == true) { image(img2,0,0); // show image 2 } else if (c3 == true) { image(img3,0,0); // show image 3 } }
the important snippet:
// detect color , show images boolean c1 = false; boolean c2 = false; boolean c3 = false; if(xrc != 0 || yrc != 0) { // color green detected c1 = true; c2 = false; c3 = false; } if(xrh != 0 || yrh != 0) { // color blue detected c2 = true; c1 = false; c3 = false; } if(xrc2 != 0 || yrc2 != 0) { // color red detected c3 = true; c1 = false; c2 = false; } if(c1 == true) { image(img,0,0); // show image 1 } else if (c2 == true) { image(img2,0,0); // show image 2 } else if (c3 == true) { image(img3,0,0); // show image 3 }
screenshots:
first object tracked , image shown
second object tracked , image shown
third object tracked , image shown
my problem: (the first object should tracked , first image should me shown)
hummm... without running code, i'd bet problem rely on coordinates (xrc
, it's siblings) being 0 choose image use. initiated 0, first run goes fine, but... never reset them zero, you? after have being changed once detecting 3 colors test became useless. perhaps can reset them 0 whenever color detected.
and maybe don't need boolean @ all...
what think of this?
pseudo
//global pimage imgs = new pimage[3]; int imagetodispaly = 0; //all stuff... if(xrc != 0 || yrc != 0) { // color green detected // not sure work, idea thing this. xrh = yrh = xrc2 = yrc2 = 0; imagetodispaly = 0; } if(xrh != 0 || yrh != 0) { // color blue detected xrc = yrc = xrc2 = yrc2 = 0; imagetodispaly = 1; } if(xrc2 != 0 || yrc2 != 0) { // color red detected xrh = yrh = xrc = yrc = 0; imagetodispaly = 2; } // @ appropriated time... image(imgs[imagetodispaly], x, y);
Comments
Post a Comment