How I can only move colored objects that I clicked on.

edited June 2017 in Library Questions

i used import video. How I can only move colored objects that I clicked on. Not moving in a similar color. When I click on an object with a specific color, I want to make a circle on the object and move the object so that the circle moves. http://learningprocessing.com/examples/chp16/example-16-11-ColorTrack // Example 16-11: Simple color tracking

import processing.video.*;

// Variable for capture device
Capture video;

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

void setup() {
  size(320, 240);
  video = new Capture(this, width, height);
  video.start();
  // Start off tracking for red
  trackColor = color(255, 0, 0);
}

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

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

  // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  float worldRecord = 500; 

  // XY coordinate of closest color
  int closestX = 0;
  int closestY = 0;

  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;
      // What 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;
      }
    }
  }

  // We only consider the color found if its color distance is less than 10. 
  // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  if (worldRecord < 10) { 
    // Draw a circle at the tracked pixel
    fill(trackColor);
    strokeWeight(4.0);
    stroke(0);
    ellipse(closestX, closestY, 16, 16);
  }
}

void mousePressed() {
  // Save color where the mouse is clicked in trackColor variable
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
}

In the example above, clicking on a specific color will continue to move to a color similar to that color. But I want to move only objects that have exactly the color I clicked, not similar colors.

I finished 'learning processing' example10 only. So I do not know those video related codes... i'm korean but all lecture videos are in English....so i can't understand... therefore i question here....

Tagged:

Answers

  • edited July 2017
    // 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;
          }
    

    change this part(upper) to this(lower) i think it will work

    if(r1==r2&&g1==g2&&b1==b2)
    {
         closestX = x;
         closestY = y;
    }
    
  • edited July 2017

    I want to move only objects that have exactly the color I clicked, not similar colors

    Another approach is to just use the code as-is. There is literally a note in the source code explaining what to change and how.

     // We only consider the color found if its color distance is less than 10. 
     // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
     if (worldRecord < 10) {
    

    So, according to the note in the source code explaining how to do this, change the "<10" to a "<1" or "<1" or "==0".

  • I'd move lines 42-44 before line 33 - you don't need to recalculate them for every pixel.

    (In fact you don't even have to recalculate them every frame, only when the tracking colour changes)

Sign In or Register to comment.