Justin
YaBB Newbies
Offline
Posts: 13
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); } }