Question_color tracking

edited March 25 in Library Questions

Hi there, I'm working on my project to draw a line based on color detecting.

what I want to do is drawing color lines when I click the color I want to track on. But when I try it, the first line's starting point is always located in the left corner. I want to make a line start from clicking points.

I think I have to correct xPrev and yPrev. But.. I don't know how to correct them.

Here is my code.

import processing.video.*; 
Capture video; 
PGraphics topLayer; 
color trackColor; 
int xprev=mouseX,yprev=mouseY;// why?

void setup() { 
  size(640, 480); 
  smooth();
  video = new Capture(this, width, height);
  video.start(); 
  trackColor = color(0, 255, 0);
  topLayer = createGraphics(width, height, g.getClass().getName());
} 

void draw() { 

  frameRate(3000);
  if (video.available()) { 
    video.read();
  } 
  video.loadPixels(); 
  image(video, 0, 0); 
  float worldRecord = 500; 
  int closestX = 0; 
  int closestY = 0; 
  for (int x = 0; x < video.width; x ++ ) { 
    for (int y = 0; y < video.height; y ++ ) { 
      int loc = x + y*video.width; 
      color currentColor = video.pixels[loc]; 
      float r1 = red(currentColor); 
      float g1 = green(currentColor); 
      float b1 = blue(currentColor); 
      float r2 = red(trackColor); 
      float g2 = green(trackColor); 
      float b2 = blue(trackColor); 


      float d = dist(r1, g1, b1, r2, g2, b2); 
      if (d < worldRecord) { 
        worldRecord = d; 
        closestX = x; 
        closestY = y;
      }
    }
  }


  topLayer.beginDraw();
  fill(trackColor);
  topLayer.stroke(trackColor);
  if (worldRecord < 10) { 
   topLayer.strokeWeight(3);  
    topLayer.line(xprev,yprev,closestX,closestY);
    xprev = closestX;
    yprev = closestY;
  }
  topLayer.endDraw();
  image(topLayer,0,0);
}

void mousePressed() { 
  int loc = mouseX + mouseY*video.width; 
  trackColor = video.pixels[loc];
}
Tagged:

Answers

  • Answer ✓

    here is an working example.

    MouseX and MouseY will always be 0 before setup() because they are not set yet.

    by giving xprev=-1, yprev=-1 values outside the range that the mouse can have, we can later check if we are at the beginning and then skip the first line from -1,-1

    see also

    https://www.processing.org/reference/pmouseX.html

    Chrisir

    PGraphics topLayer; 
    color trackColor; 
    int xprev=-1, yprev=-1; // -1 means undefined 
    
    void setup() { 
      size(640, 480); 
      smooth();
    
      trackColor = color(0, 255, 0);
      topLayer = createGraphics(width, height, g.getClass().getName());
    } 
    
    void draw() { 
      makeImage() ; 
      image(topLayer, 0, 0);
    }
    
    void makeImage() {
      topLayer.beginDraw();
      fill(trackColor);
      topLayer.stroke(trackColor);
    
      topLayer.strokeWeight(3);
      if (mousePressed)
        if (!(xprev==-1||yprev==-1))
          topLayer.line(xprev, yprev, mouseX, mouseY);
      xprev = mouseX;
      yprev = mouseY;
    
      topLayer.endDraw();
    }
    
    //
    
  • thank you Chrisir! it works well :)

Sign In or Register to comment.