OPENCV COLOR TRACKER
- import hypermedia.video.*;
- OpenCV opencv;
- color trackColor;
- PImage t;
- void setup() {
- size(480, 360);
- opencv = new OpenCV(this);
- opencv.capture(width, height); // open video stream
- trackColor = color(255, 0, 0);
- }
- void draw() {
- background(0);
- opencv.read();
- image(opencv.image(),0,0);
- float worldRecord = 500;
- int closestX = 0;
- int closestY = 0;
- t = opencv.image();
- for (int x = 0; x < opencv.width; x ++ ) {
- for (int y = 0; y < opencv.height; y ++ ) {
- int loc = x + y*opencv.width;
- color currentColor = t.pixels[loc];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- float d = dist(r1, g1, b1, r2, g2, b2);
- if (d < worldRecord) {
- worldRecord = d;
- closestX = x;
- closestY = y;
- }
- }
- }
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX, closestY, 16, 16);
- }
- }
- void mousePressed() {
- int loc = mouseX + mouseY*opencv.width;
- trackColor = t.pixels[loc];
- }