thanks again! feel free to check out the code below. i am much on my way to finishing it! the only feature left to add is the ability to set the color to be tracked by a click on a pixel with my mouse.
Code:
/**
* 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();
color colorRead=color(0,255,0); //this is the color we will be searching for
int index = 0;
colorMode(RGB);
int scanHeight=10; //how wide would you like to scan?
index = ((((height/2) - (scanHeight/2)) * width) - 1); //set this for first index value
color Getcolor=get(mouseX,mouseY);
//getting color by mouse click
if (mousePressed){ //if mouse is pressed, we show a different color cross hair and perform color catching action
video.read();
color Get = get(mouseX,mouseY);
rectMode(CENTER);
stroke(Getcolor);
noFill();
rect(mouseX,mouseY,52,52);
}
for(int y = ((height/2) - (scanHeight/2)); y < ((height/2) + (scanHeight/2)); 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)-red(colorRead))+abs(green(pixelValue)-green(colorRead))+abs(blue(pixelValue)-blue(colorRead));
// 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;
colorY = y;
colorX = x;
}
index++;
}
}
// Draw a large, circle at the closest in value pixel
fill(colorRead, 128); //it will fill the circle as the color you want.
noStroke();
ellipse(colorX, colorY, 50, 50);
//print the location of the color found
print("X: ");
println(colorX);
print("Y: "); //this doesn't really matter, but doesn't slow the program.
println(colorY);
//mousing setup - we will draw a crosshairs on where the mouse is.
rectMode(CENTER);
stroke(0);
noFill();
rect(mouseX,mouseY,50,50);
rect(mouseX,mouseY,5,5);
//draw a rectangle around the area we are scanning
rectMode(CENTER);
stroke(255,3,239); //bright pink
noFill();
rect(width/2,height/2,width,scanHeight);
//draw a rectangle in the bottom corner to show what color is being picked up by the mouse location of video feed
stroke(0);
fill(Getcolor);
rect(width-45,height-45, 90,90);
}
}