Multi-color Tracking from Webcam, Arrays as PVectors

Hi,

I am having trouble figuring out how to translate this. I am trying to make something that tracks pixels of a certain color based on their location and draws a model on that location. This is easy to do with one model and one pixel color, following Shiffman's example (http://www.learningprocessing.com/examples/chapter-16/example-16-11/) but I am trying to be able to do this three times, so the user can click up to three different colors and it will display a different object per color. The place I am held up with is converting the PVector trackColor[] (which is also an Array) into the color and location that the mousePressed && KeyPressed is. I've tried reformatting it but don't know what I am doing wrong with regard to assigning the array index those 3 variable. I get various error messages from missing a right parenthesis, to cannot convert Pvector to int.....

Here's the code,

you can substitute your own .obj models or simply erase the pushMatrix() stuff and draw an ellipse using the same variables??

Any advice is much appreciated!!!!


import saito.objloader.*;
import processing.video.*;


OBJModel model0, model1, model2;
Capture video;


int[] models = new int[3]; //average of pixel locations tracked, where models should be placed, this should be a PVector???

PVector[] trackColor= new PVector[5];  //color as a location in the video where it is tracked, I want to use a standard z value

int Assigned = 0; //# of mousepressed event/assigned pixel colors to toggle on/off

int mouseColor = 0; //sample of the color where mouse is pressed

void setup() {
  size(screen.width, screen.height, P3D);

  model0 = new OBJModel(this, "planque1.obj");
  model0.enableDebug();
  model0.translateToCenter();

  model1 = new OBJModel(this, "planque2.obj");
  model1.enableDebug();
  model1.translateToCenter();

  model2 = new OBJModel(this, "planque4.obj");
  model2.enableDebug();
  model2.translateToCenter();




  video = new Capture(this,width,height,30);

}



void draw() {

  if (video.available()) {
    video.read();
  }
  video.loadPixels();
  image(video,0,0);
  background(video);


  for (int x = 0; x < video.width; x++) {
    for (int y = 0; y < video.height; y++ ) {
      //calculate the 1D location from a 2D grid
      int loc = x + y*video.width;
      //get the R,G,B values from image
      color currentColor = video.pixels[loc];
      float r1 = red   (currentColor);
      float g1 = green (currentColor);
      float b1 = blue  (currentColor);
      float r2 = red (mouseColor);
      float g2 = green(mouseColor);
      float b2 = blue(mouseColor);

      float d= dist(r1, g1, b1, r2, g2, b2);



      if ((Assigned>0) && (currentColor== mouseColor)) {

        pushMatrix();
        translate(width/2, height/2, 1);
        model0.draw();
        popMatrix();
      }



      if ((Assigned>0) && (currentColor== mouseColor)) {

        pushMatrix();
        translate(trackColor[1].x, trackColor[1].y, trackColor[1].z);
        model1.draw();
        popMatrix();

    }
      if ((Assigned>0) && (currentColor== mouseColor)) {

        pushMatrix();
        translate(trackColor[2].x, trackColor[2].y, trackColor[2].z);
        model2.draw();
        popMatrix();

    }



  }
  }
}

void mousePressed() {  

  //get location of mouse press :
   int loc = mouseX + mouseY*video.width;
    mouseColor = video.pixels[loc];

    //depending on keypressed:
   if(mousePressed && keyPressed && keyCode == '1');
      trackColor[0] = mouseColor.get(); //get mousecolor location, and set it as the #index in the PVector array, this is the problem!!!
       Assigned= 1;
      if(mousePressed && keyPressed && keyCode == '2');
        trackColor[1]=  mouseColor;
        Assigned=2;
      if(mousePressed && keyPressed && keyCode == '3');
         trackColor[2]= mouseColor;
      Assigned=3;
     if(mousePressed && keyPressed && keyCode == SHIFT);
       Assigned = 0;




}  

Answers

  • Sorry, first time i reply here, beside the code is a bit messy, but it works :p

  • edited July 2014

    Alright, i don't know what's wrong with my computer but i couldn't post the code correctly...I worked on a similar project, here's the code i made based on the previous tutorial, i track four colors chosen by a right click of the mouse, you can predefine them...

    //Tracking four colors, each color is selected by a right mouse click

    //By HARIK el Houssein Chouaib //2014

    import processing.video.*;
    import hypermedia.net.*;
    
    
    Capture video; 
    final int TOLERANCE = 20;
    
    float XRc = 0;// XY coordinate of the center of the first target
    float YRc = 0;
    float XRh = 0;// XY coordinate of the center of the second target
    float YRh = 0;
    float XRc2 = 0; // XY coordinate of the center of the third target
    float YRc2 = 0;
    float XRh2 = 0;// XY coordinate of the center of the fourth target
    float YRh2 = 0;
    
    int ii=0; //Mouse click counter
    
    color trackColor; //The first color is the center of the robot 
    color trackColor2; //The second color is the head of the robot
    color trackColor3; //The first color is the center of the robot 2
    color trackColor4; //The second color is the head of the robot 2
    
    void setup() {
    
    size(640,480);
    video = new Capture(this,640,480);
    video.start();
    
    trackColor = color(255,0,0);
    trackColor2 = color(255,0,0);
    trackColor3 = color(255,0,0);
    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;
          }
    
      }
      }
    
    
    
    
      if(XRc != 0 || YRc != 0) { // Draw a circle at the first target
        fill(trackColor);
        strokeWeight(0.05);
        stroke(0);
        ellipse(XRc,YRc,20,20);
      }
    
    
      if(XRh != 0 || YRh != 0) {// Draw a circle at the second target
        fill(trackColor2);
        strokeWeight(0.05);
        stroke(0);
        ellipse(XRh,YRh,20,20);
      }
    
      if(XRc2 != 0 || YRc2 != 0) {// Draw a circle at the third target
        fill(trackColor3);
        strokeWeight(0.05);
        stroke(0);
        ellipse(XRc2,YRc2,20,20);
      }
    
    
       if(XRh2 != 0 || YRh2 != 0) {// Draw a circle at the fourth target
        fill(trackColor4);
        strokeWeight(0.05);
        stroke(0);
        ellipse(XRh2,YRh2,20,20);
      }
    
    }
    void mousePressed() {
      if (mousePressed && (mouseButton == RIGHT)) { // Save color where the mouse is clicked in trackColor variable  
      if(ii==0){  
    
        if (mouseY>480){mouseY=0;mouseX=0;}
      int loc = mouseX + mouseY*video.width;
    
      trackColor = video.pixels[loc];
    ii=1;  
    }
      else if(ii==1){  
        if (mouseY>480){mouseY=0;mouseX=0;}
      int loc2 = mouseX + mouseY*video.width;
      trackColor2 = video.pixels[loc2];
    ii=2;  
    }
      else if(ii==2){  
        if (mouseY>480){mouseY=0;mouseX=0;}
      int loc3 = mouseX + mouseY*video.width;
      trackColor3 = video.pixels[loc3];
    ii=3;  
    }
      else if(ii==3){  
        if (mouseY>480){mouseY=0;mouseX=0;}
      int loc4 = mouseX + mouseY*video.width;
      trackColor4 = video.pixels[loc4];
    ii=0;  
    }
    
    
      }
      }
    
  • Answer ✓

    Merci!!!! its working just figuring out some OBJloader stuff now, although still curious what was wrong with array/PVector situation....

    Thank you!!!!!!

  • @Chouaib! In order to post code correctly formatted for the forum, highlight it and hit CTRL+K. Thx! =:)

  • edited July 2014

    @julia789 You're welcome :)

    @GoToLoop Thank you very much, i edited the post :)

Sign In or Register to comment.