detecting color

JaiJai
edited April 2016 in Questions about Code

ok so in this photo i have rectangles all over the place, now my question is

how do we determine if a color is behind any of those rectangles ?

WIN_20160229_134722

for example lets say i have my video screen from my webcam open right, and lets say im pointing at a white wall "absolute white" and lets say i bounce a red or black ball across the webcams view so now you should see a white background with a color ball in it right, now how can i detect that ball ?

by detecting i mean detect & track it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

now i dont just want to track the ball but i want to guide the cursor of my mouse across the screen.

void draw()
{
    image(video, 0, 0 );
    noFill();
    stroke(255, 0, 0);
    ellipse(300, 300, 20, 20);
}

using this color tracker and incorporating the mouseMove functions i think i can get what im looking for, the Q is where do i get a example of mouse move? i forgot where i seen it.

  float worldRecord = 100.0;//500 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.
  // 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 < 95.73401)//10 
  { 
    // Draw a circle at the tracked pixel
    fill(trackColor);
    strokeWeight(4.0);
    stroke(0);
    ellipse(closestX, closestY, 16, 16);
  }
Tagged:

Answers

  • Did you look at the libraries ?

    Opencv

  • JaiJai
    edited April 2016

    i actually figured it out soon after this post lol sorry about that Christopher i got it working already thanks tho!

    i just gotta figure out HOW to say if this color shows up here track it if it doesn't then dont do nothing

  • @Chrisir how do we flip the screen from the draw window while running the live feed? i know im supposed to input a -value somewhere but is not in image(video, -1, 0);

    or

    fill(trackColor); strokeWeight(4.0); stroke(0); ellipse(closestX, closestY, 16, 16);

  • scale(0,-1);

    ??

  • JaiJai
    edited April 2016

    @Chrisir that did not work in fact i got i got an error for that one, saying "PRError: setPenT4: invalid pen trasnformation(singular)"

    Capture

    my issue is that my mouse is going the opposite way sorta like a mirror effect when i go either left or right, up and down is correct is the side to side

  • maybe i should be looking into changing the values of the trackColor?>

Sign In or Register to comment.