import processing.video.*; Capture video; color col; int tolerance = 20; PFont font; String desc = "Actual tracing brightest pixel ..."; void setup() { size(320, 270); // define window size video = new Capture(this, width, 240, 30); // define capture size noStroke(); smooth(); cursor(CROSS); // set mouse cursor to cross font = loadFont("HelveticaNeue-Medium-12.vlw"); // load font to display text textFont(font); fill(255); textSize(12); } void draw() { if (video.available()) { video.read(); image(video, 0, 0, width, 240); // Draw the webcam video onto the screen int brightestX = 0; // X-coordinate of the brightest video pixel int brightestY = 0; // Y-coordinate of the brightest video pixel float brightestValue = 0; // Brightness of the brightest video pixel float nearestValue = 500; // set value that is easy to beat // Search for the brightest 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(); int index = 0; for (int y = 0; y < video.height; y++) { for (int x = 0; x < video.width; x++) { // Get the color stored in the pixel int pixelValue = video.pixels[index]; // Determine the brightness of the pixel float pixelBrightness = brightness(pixelValue); // If that value is brighter than any previous, then store the // brightness of that pixel, as well as its (x,y) location if (col == 0) { if (pixelBrightness > brightestValue) { brightestValue = pixelBrightness; brightestY = y; brightestX = x; } } else { float r1 = red(color(pixelValue)); float g1 = green(color(pixelValue)); float b1 = blue(color(pixelValue)); float r2 = red(col); // track color float g2 = green(col); // track color float b2 = blue(col); // track color // Using euclidean distance to compare colors float d = 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 (d < nearestValue) { nearestValue = d; brightestX = x; brightestY = y; } } index++; } } // Draw a large, yellow circle at the brightest pixel fill(255, 204, 0, 128); ellipse(brightestX, brightestY, 50, 50); } fill(0); rect(0, 240, width, 30); fill(255); text(desc, 4, 250, width, 30); } void keyPressed() { if (keyCode == UP) { if (tolerance >= 100) { tolerance = 100; } else { tolerance += 1; } desc = "Tolerance set to: " + tolerance; } if (keyCode == DOWN) { if (tolerance <= 0) { tolerance = 0; } else { tolerance -= 1; } desc = "Tolerance set to: " + tolerance; } if (key == 'r') { col = 0; desc = "Color reset ... tracing brightest pixel ..."; } } void mouseClicked() { // save picked up pixel color if (mouseButton == LEFT) { col = get(mouseX,mouseY); //System.out.println("Red: " + red(col) + " / Green: " + green(col) + " / Blue: " + blue(col)); desc = "Tracing color (R: " + red(col) + " / G: " + green(col) + " / B: " + blue(col) + ")"; // reset picked up color } else if (mouseButton == RIGHT) { col = 0; desc = "Color reset ... tracing brightest pixel ..."; } }