How to make the ellipse stay on captured colour - video cam

edited January 9 in Library Questions

I want achive that with everz click on colour, there will appear and staz ellipse, capturing the colour/not to disappear. Is there some easier way than to create separate drawing loops for different ellipses? Just to say something like capture and stay, even if I click again somewhere else? (example from Levin and Shiffman)

import processing.video.*;

Capture video;

// A variable for the color we are searching for.
color trackColor; 

void setup() {
  size(640, 480);
  video = new Capture(this, width, height);
  video.start();
  // Start off tracking black
  trackColor = color(0);
}

void captureEvent(Capture video) {
  // Read image from the camera
  video.read();
}

void draw() {
  video.loadPixels();
  image(video, 0, 0);

//more higher more arger chance to find the colour 
  float worldRecord = 500; 

// seek for the closest color
  int closestX = 0;
  int closestY = 0;

// search every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;
// define and showWhat is current color
      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);

// Using euclidean distance to compare colors
      float d = dist(r1, g1, b1, r2, g2, b2); // We are using the dist( ) function to compare the current color with the color we are tracking.

      // If current color is more similar to tracked color than
      // closest color, save current location and current difference
      if (d < worldRecord) {
        worldRecord = d;
        closestX = x;
        closestY = y;
      }
    }
  }

  // trackcolour not more far than 20 
    if (worldRecord < 20) { 
    // Draw a circle at the tracked pixel
    fill(trackColor);
    strokeWeight(4.0);
    noStroke();
    ellipse(closestX, closestY, 60, 60);
  }
}

void mousePressed() {
  // store the colour from video and keep it tracked
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
}

Answers

  • Answer ✓

    are you referring to line numbers 62 to 68 ?

  • Yes. I wanted to create that everytime i click and create ellipse which is tracking the pixel colour, will stay there, tracking and I will with every click add a new one. thanks that you wrote.

  • Similar/relevant: https://forum.processing.org/two/discussion/comment/114336/#Comment_114336

    Some code:

    class ColorTracker{
      final int RAD=60;
      color col;
      PVector p;
    
      ColorTracker(float x, float y, color c){
        col=c;
        p=new Pvector(x,y);
      }
    
      void show(){
        fill(col);
        ellipse(p.x,p.y, RAD, RAD);
      }
    }
    
    ArrayList<ColorTracker> points_Im_tracking= new ArrayList();
    

    Repalce line 67 with:

     points_Im_tracking.add(new ColorTracker(closestX, closestY,trackColor));
    

    At the end of draw():

    for(ColorTracker ct:points_Im_tracking){
      ct.show();
    }
    

    Kf

Sign In or Register to comment.