1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
// Smoother color tracking // based on Dan Shiffman & Dan O'Sullivan's examples import processing.video.*; // Variable for capture device Capture video; PGraphics drawing; /// a contect to hold the "drawing" PImage vidMirror; // A variable for the color we are searching for. color trackColor; float thresh = 20; int avgX, avgY; //this is what we are trying to find int prevAvgX, prevAvgY; void setup() { size(640, 480); video = new Capture(this, width, height); video.start(); // Start off tracking for red trackColor = color(255, 0, 0); // instance of the drawing drawing = createGraphics(width, height); vidMirror = new PImage(video.width, video.height); } void draw() { // Capture and display the video if (video.available()) { video.read(); } video.loadPixels(); // image(video, 0, 0); for(int x = 0; x < video.width; x++){ for(int y = 0; y < video.height; y++){ vidMirror.pixels[x+y*video.width] = video.pixels[(video.width-(x+1))+y*video.width]; } } vidMirror.updatePixels(); image(vidMirror,0,0); //we are going to find the average location of changed pixels so //we will need the sum of all the x find, the sum of all the y // find and the total finds int totalFoundPixels= 0; int sumX = 0; int sumY = 0; // Begin loop to walk through every pixel for (int x = 0; x < vidMirror.width; x ++ ) { for (int y = 0; y < vidMirror.height; y ++ ) { int loc = x + y*vidMirror.width; // What is current color color currentColor = vidMirror.pixels[loc]; float r1 = red(currentColor); float g1 = green(currentColor); float b1 = blue(currentColor); // colors we are looking for float r2 = red(trackColor); float g2 = green(trackColor); float b2 = blue(trackColor); // Using euclidean distance to compare colors float difference = dist(r1, g1, b1, r2, g2, b2); // We are using the dist( ) function to compare the current // color with the color we are tracking. // If current color is more similar to tracked color than // closest color, save current location and current difference if (difference < thresh) { sumX = sumX + x; sumY= sumY + y; totalFoundPixels++; } } } // average the locations of all the pixels that were close enough. if (totalFoundPixels > 0) { avgX = sumX/totalFoundPixels; avgY = sumY/totalFoundPixels; // Draw a circle at the tracked pixel fill(trackColor); strokeWeight(4.0); stroke(0, 127); ellipse(avgX-10, (avgY-10), 20, 20); } drawing.beginDraw(); // Draw a large, yellow circle at the brightest pixel drawing.stroke(trackColor); // drawing.noStroke(); strokeWeight(3); drawing.line(prevAvgX, prevAvgY, avgX, avgY); drawing.endDraw(); blend(drawing, 0, 0, width, height, 0, 0, width, height, ADD); prevAvgX = avgX; prevAvgY = avgY; } void mousePressed() { // Save color where the mouse is clicked in trackColor variable int loc = mouseX + mouseY*vidMirror.width; trackColor = vidMirror.pixels[loc]; } void keyPressed() { //for adjusting things on the fly if (key == '-') { thresh--; println("Threshold " + thresh); } else if (key == '=') { thresh++; println("Threshold " + thresh); } } |