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.
IndexProgramming Questions & HelpPrograms › Color Tracking.
Page Index Toggle Pages: 1
Color Tracking. (Read 3241 times)
Color Tracking.
Apr 16th, 2009, 2:25pm
 
First time posting, and haven't been using Processing for too long. Long story short; I'm trying to take in live video and find and track an area of the video image that is closest in color value to a specified color.

The use will be for a line tracking bot: the camera will be pointed directly at the ground ahead of the bot. I wish for processing to track the line (and thereby steer the bot). I want it to pick up on the color of the line, and not just the line itself because I will eventually want intersections and many lines of different colors for different bots.

I have attempted to modify the BrightnessTracking example included with the software. Below is a copy of my code.

Does anyone have any tips or tricks, or can see an error in my code?

//begin code

import processing.video.*;

Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, width, height, 30);
 noStroke();
 smooth();
 int colors=color(54,255,0); //we define the color that we want the bot to track.
}

void draw() {
 if (video.available()) {
   video.read();
   image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int brightestX = 0; // X-coordinate of the brightest video pixel
   int brightestY = 0; // Y-coordinate of the brightest video pixel
   float brightestValue = 0; // Brightness of the brightest video pixel
   // Search for the brightest pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video
   video.loadPixels();
   int index = 0;
     for (int x = 0; x < video.width; x++) {
       int y=height/2;
       // Get the color stored in the pixel
       int pixelValue = video.pixels[index];
       // Determine the brightness of the pixel
       float pixelBrightness = color(pixelValue);
       // If that value is brighter than any previous, then store the
       // brightness of that pixel, as well as its (x,y) location
       if (abs(pixelValue-colors)) > (abs(brightestValue-colors)) {
         brightestValue = pixelBrightness;
         brightestY = y;
         brightestX = x;
       }
       index++;
     }
   // Draw a large, yellow circle at the brightest pixel
   fill(54, 255, 0, 300);
   ellipse(brightestX, brightestY, 100, 100);
 }
}

Re: Color Tracking.
Reply #1 - Apr 17th, 2009, 12:48am
 
Hi,

Several prbs in your code :

- testing the line in the middle of the video :
to do so you have to understand that the video.pixels tab is linear. To retrieve the pixel (x,y) in this tab use : video.pixels[x+y*height/2]
You don't need the index value. In your code you're currently testing the first line on the top of the video.
Are you sur your line will be right in the middle ?

- getting a color :
video.pixels[] gives you a color value. Use that code :
color currentPixel =video.pixels[];

- testing color :
If I understand well you want to track a color not brightness.
To do so you have to comapre color one to another red,green and blue.
(An HSB approach could be intresting using Hue)

To compare do : (your colors variable should be color type)
red(currentPixels)-red(colors) blue(currentPixels)-blue(colors) ...

see the book Processing "A programming handbook...." all example including video tracking here : processing.org/img/learning/Processing-Examples-001.zip

Hope this help.

Paul
Re: Color Tracking.
Reply #2 - Apr 19th, 2009, 8:09pm
 
Hey Paul, thanks for your help. I now have working code. Currently, I have it set to track green (RGB,0,255,0). Below is the code. However, there are some major problems in design, but I will work on those later. But anyway, it works for now. Check it out and let me know what you think.

/**
* Color Tracking
* by Justin Brooks
*
*
* based on original code:
*   Brightness Tracking
*   by Golan Levin.
*
* Tracks the tracks pixel closest in color to specified color.
*/


import processing.video.*;

Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, width, height, 30);
 noStroke();
 smooth();
}

void draw() {
 if (video.available()) {
   video.read();
   image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int colorX = 0; // X-coordinate of the closest in color video pixel
   int colorY = 0; // Y-coordinate of the closest in color video pixel
   float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
   // Search for the closest in color pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video
   video.loadPixels();
   int index = 0;
   for (int y = 0; y < video.height; y++) {
     for (int x = 0; x < video.width; x++) {
       // Get the color stored in the pixel
       color pixelValue = video.pixels[index];
       // Determine the color of the pixel
       float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
       // If that value is closer in color value than any previous, then store the
       // color proximity of that pixel, as well as its (x,y) location
       if (colorProximity < closestColor) {
         closestColor = colorProximity;
         closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
         colorY = y;
         colorX = x;
       }
       index++;
     }
   }
   // Draw a large, yellow circle at the brightest pixel
   fill(255, 204, 0, 128);
   ellipse(colorX, colorY, 200, 200);
 }
}
Page Index Toggle Pages: 1