/** * Brightness Tracking * by Golan Levin. * * Tracks the brightest pixel in a live video signal. */ import processing.video.*; import ddf.minim.*; AudioPlayer player; Minim minim; Capture video; PImage gun, gunshot, splat, targetR, targetB; void setup() { size(460, 350); // Change size to 320 x 240 if too slow at 640 x 480 // Uses the default video input, see the reference if this causes an error video = new Capture(this, width, height, 30); noStroke(); smooth(); minim = new Minim(this); player = minim.loadFile("shot.wav"); gun = loadImage("gun1.png"); gunshot = loadImage("gun2.png"); splat = loadImage("splat.png"); //target = loadImage("target.png"); targetB = loadImage("targetB.png"); targetR = loadImage("targetR.png"); } void draw() { if (video.available()) { video.read(); image(video, 0, 0, width, height); // Draw the webcam video onto the screen int brightestRX = 0; // X-coordinate of the brightest video pixel int brightestRY = 0; // Y-coordinate of the brightest video pixel int brightestBX = 0; // X-coordinate of the brightest video pixel int brightestBY = 0; // Y-coordinate of the brightest video pixel float brightestValue = 0; // Brightness of the brightest video pixel // 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 (pixelBrightness > brightestValue) { // brightestValue = pixelBrightness; // brightestY = y; // brightestX = x; 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 pixelBrightnessR = red(pixelValue); // Get the color stored in the pixel //int pixelValue = video.pixels[index]; // Determine the brightness of the pixel float pixelBrightnessB = blue(pixelValue); // If that value is brighter than any previous, then store the // brightness of that pixel, as well as its (x,y) location if (pixelBrightnessR > brightestValue) { brightestValue = pixelBrightnessR; brightestRY = y; brightestRX = x; } if (pixelBrightnessB > brightestValue) { brightestValue = pixelBrightnessB; brightestBY = y; brightestBX = x; } index++; } } // Draw a large, yellow circle at the brightest pixel //fill(255, 204, 0, 128); //ellipse(brightestX, brightestY, 200, 200); //blue image (targetB, brightestBX, brightestBY); image (gun, brightestBX, 200); //red image (targetR, brightestRX, brightestRY); image (gun, brightestRX, 200); // image (target, brightestX, brightestY); // image (gun, brightestX, 200); //red if (keyPressed == true) { //blue image (gunshot, brightestBX, 200); image (splat, brightestBX, brightestBY); //red image (gunshot, brightestRX, 200); image (splat, brightestRX, brightestRY); player.play(); player.loop(1); // if (keyPressed == true) { // image (gunshot, brightestX, 200); // image (splat, brightestX, brightestY); // player.play(); //player.loop(); } } } void stop() { // always close Minim audio classes when you are done with them player.close(); minim.stop(); super.stop(); }