/** * Color Tracking code mostly by Daniel Schiffman from the book Learning Processing * Tracks the pixel closest in color to specified color and makes a sound depending in which quadrant the color is in */ //////////////////////MAGIC GLOVE////////////////////////////////////////////////////// /////////////////////put on a yellow glove and play music in the air////////////////////// import codeanticode.gsvideo.*; import ddf.minim.*; Minim sound; AudioSample[] snip = new AudioSample[15]; //i can choose between 15 audio snippets GSCapture video; boolean myswitch1=true; boolean myswitch2=true; boolean myswitch3=true; boolean myswitch4=true; color trackColor = color(255,255,0); //red and green make yellow int cellSize = 20; //pixel size. change for different sized pixels. int cols, rows; void setup() { size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480 frameRate(30); video = new GSCapture(this, width, height, 30); cols = width / cellSize; rows = height / cellSize; smooth(); sound = new Minim(this); for (int i = 1; i < snip.length; i++) { snip[i] = sound.loadSample("samp" + i + ".wav"); } } void draw() { //background(0,0,255); //can have a blue background instead of video stroke(0); float one = width*.25; float two = width*.5; float three = (width*.75)-1; line(one, 0, one ,height); line(two, 0, two, height); line(three, 0, three, height); if (video.available()) { video.read(); //image(video, 0, 0, width, height); // leave in for testing // Begin loop for columns //DS Code for mirror with pixels. mirror so it looks right on the screen for (int i = 0; i < cols; i++) { // Begin loop for rows for (int j = 0; j < rows; j++) { // pixelize for "style" int x = i*cellSize; int y = j*cellSize; int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image float r = red(video.pixels[loc]); float g = green(video.pixels[loc]); float b = blue(video.pixels[loc]); // Make a new color with an alpha component color c = color(r, g, b, 75); // Code for drawing a single rect // Using translate in order for rotation to work properly pushMatrix(); translate(x+cellSize/2, y+cellSize/2); // Rotation formula based on brightness rectMode(CENTER); fill(c); noStroke(); // Rects are larger than the cell for some overlap rect(0, 0, cellSize, cellSize); popMatrix(); } }///end DS code int closestX = 0; // X-coordinate of the closest in color video pixel int closestY = 0; // Y-coordinate of the closest in color video pixel float worldRecord = 500; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value // Search for the closest in color 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 color currentColor = video.pixels[index]; 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 that value is closer in color value than any previous, then store the // color proximity of that pixel, as well as its (x,y) location if (d < worldRecord) { worldRecord = d; worldRecord = worldRecord-10; //change the 10 number for different accuracy closestX = x; closestY = y; } index++; } } // Draw a yellow circle at the brightest pixel int flipx = video.width - closestX - 1; fill(255, 204, 0, 128); noStroke(); ellipse(flipx, closestY, 30, 30); //logic to get the yellow circle to play a unique note once during entry in to each chamber if ((closestX < one)&&(closestX>0)) { if (myswitch1) { println("FIRST CHAMBER"); snip[1].trigger(); myswitch1=false; myswitch2=true; myswitch3=true; myswitch4=true; } } if ((closestX > one)&&(closestXtwo)&&(closestXthree)&&(closestX