TUTORIAL: Webcam Color Tracking
To make this work, you have to connect a USB or firewire camera to your
computer.
This program scans the video-image for the color specified in the trackRGB
cariable. The video feed is displayd on the canvas with a cross drawn
where the color is found.
- - - -
Point tracker = new Point(0, 0);
Color trackRGB = new Color(200, 200, 200);
void setup() {
size(120, 160);
beginVideo(120, 160, 12);
}
void loop(){
image(video, 0, 0);
cross(tracker.x, tracker.y, 5, 255, 0, 0);
}
void videoEvent(){
for(int j=0; j<video.height; j++){
for(int i=0; i<video.width; i++){
int num = (j*video.width)+i;
if(red(video.pixels[num]) > trackRGB.getRed() && green(video.pixels[num])
> trackRGB.getGreen() && blue(video.pixels[num]) > trackRGB.getBlue()){
tracker.x = i;
tracker.y = j;
}
}
}
}
void cross(int xx, int yy, int dim, int rr, int gg, int bb){
stroke(rr, gg, bb);
line(-dim+xx, yy, dim+xx, yy);
line(xx, dim+yy, xx, -dim+yy);
}
|