Processing Forum
import processing.video.*;
Capture cam;
color trackColor = color(0,0,255);;
int closest = 0;
int x = 0;
int y = 0;
void setup() {
size(320,240);
cam = new Capture(this,320,240);
}
void draw() {
if (cam.available()) {
cam.read();
}
cam.loadPixels();
float bestScore = 50000; // heel groot getal
for (int i = 0; i < cam.pixels.length; i ++ ) {
float distance = colorDist(cam.pixels[i], trackColor);
if (distance < bestScore) {
bestScore = distance;
closest = i;
}
}
image(cam,0,0);
fill(200,200,200);
x = closest % cam.width;
y = (closest - x) / cam.width;
ellipse(x,y,16,16);
fill(0,0,0);
rect(x,y,20,40);
}
void mousePressed() {
int loc = mouseX + mouseY*cam.width;
trackColor = cam.pixels[loc];
}
float colorDist(color c1, color c2){
return dist(red(c1), green(c1), blue(c1), red(c2), green(c2), blue(c2));
}