We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Drawing via Color Tracking? (Read 1000 times)
Drawing via Color Tracking?
Apr 20th, 2010, 8:08am
 
I'm new to processing and I need some help with combining simple color tracking and drawing by Daniel Shiffman. Here are the example codes I'm trying to use:

Color Tracking
// Learning Processing
// Daniel Shiffman

// 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,15);
 // Start off tracking for red
 trackColor = color(255,0,0);
 smooth();
}

void draw() {
 // Capture and display the video
 if (video.available()) {
   video.read();
 }
 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];
}


Drawing
// Learning Processing
// Daniel Shiffman

// Example 3-4: Drawing a continuous line
void setup() {
 size(200, 200);
 background(255);
 smooth();
}

void draw() {
 stroke(0);
 // Draw a line from previous mouse location to current mouse location.
 line(pmouseX, pmouseY, mouseX, mouseY);
}


Is that possible to combine both color tracking and drawing into one program? Any help will be appreciated.

Thanks!
Re: Drawing via Color Tracking?
Reply #1 - Apr 22nd, 2010, 4:46am
 
Interesting..... I am trying to do something similar, but I like that code.

I haven't tried it yet, but you might be able to set the mousex and mousey vars... as long as you don't move the mouse. Try this:
Code:

if (worldRecord < 10) {
  // Draw a circle at the tracked pixel
  fill(trackColor);
  strokeWeight(4.0);
  stroke(0);
  mousex = closestX;
  mousey = closestY;

  ellipse(mousex ,mousey ,16,16);
  line(pmouseX, pmouseY, mouseX, mouseY);


}


Although, i looking at that, I suppose you wouldn't really need to set the mousex and mousey, just draw a line.

The hard part would be the "mousePressed" or stopping/starting drawing somehow. Unfortunately, there isn't really any way to check the distance with one camera..... I think.  Undecided

Post back if that works ok!
Happy Thursday,
  --- squirrel
Page Index Toggle Pages: 1