import processing.video.*; Capture myCapture; int mode = 0; int numPixels; int[] previousFrame; void setup() { size(1024, 768); background(0); smooth(); myCapture = new Capture(this, width, height, 30); //myCapture.loadPixels(); // The name of the capture device is dependent those // plugged into the computer. To get a list of the // choices, uncomment the following line // println(Capture.list()); // And to specify the camera, replace "Camera Name" // in the next line with one from Capture.list() // myCapture = new Capture(this, width, height, "Camera Name", 30); numPixels = myCapture.width * myCapture.height; previousFrame = new int[numPixels]; loadPixels(); } void captureEvent(Capture myCapture) { myCapture.read(); } void draw() { if (mode == 1){ filter1(); } else if (mode == 2){ filter2(); } else if (mode == 3){ filter3(); } else if (mode == 4){ filter4(); } else if (mode == 5){ filter5(); } else if (mode == 6){ filter6(); } else if (mode == 7){ filter7(); } } void keyPressed() { if (key == '1') { mode = 1; } else if (key == '2'){ mode = 2; } else if (key == '3'){ mode = 3; } else if (key == '4'){ mode = 4; } else if (key == '5'){ mode = 5; } else if (key == 'b'){ mode = 6; } else if (key == 'w'){ mode = 7; } } void filter1(){ image(myCapture, 0, 0); } void filter2(){ //image(myCapture, 0, 0); //circle filter for (int i = 0; i < width*height; i += 32){ color p = myCapture.pixels[i]; float r = red(p); float g = green(p); float b = blue(p); //float bwx = (r + g + b) /3; float circle = map(b, 0, 255, 0, 50); int x = i % width; int y = i / width; if (y % 5 == 0){ fill((r+g+b/255)); ellipse(x, y, circle, circle); } } } void filter3(){ //image(myCapture, 0, 0); //circle filter for (int i = 0; i < width*height; i += 32){ color p = myCapture.pixels[i]; float r = red(p); float g = green(p); float b = blue(p); //float bwx = (r + g + b) /3; float circle = map(b, 0, 255, 0, 50); int x = i % width; int y = i / width; if (y % 5 == 0){ fill(r, g, b); ellipse(x, y, circle, circle); } } } void filter4(){ if (myCapture.available()) { // When using video to manipulate the screen, use video.available() and // video.read() inside the draw() method so that it's safe to draw to the screen myCapture.read(); // Read the new frame from the camera myCapture.loadPixels(); // Make its pixels[] array available int movementSum = 0; // Amount of movement in the frame for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... color currColor = myCapture.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel int currR = (currColor >> 16) & 0xFF; // Like red(), but faster int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel int prevR = (prevColor >> 16) & 0xFF; int prevG = (prevColor >> 8) & 0xFF; int prevB = prevColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = abs(currR - prevR); int diffG = abs(currG - prevG); int diffB = abs(currB - prevB); // Add these differences to the running tally movementSum += diffR + diffG + diffB; //render difference to grey if(((diffR + diffG + diffB) > 50)){ pixels[i] = color(100); //if both are different, render red if(((diffR + diffG + diffB) > 150) && ((prevR + prevG + prevB) > 150)){ pixels[i] = color(255, 0, 0); } } else { pixels[i] = color(255); } // Render the difference image to the screen //pixels[i] = color(diffR, diffG, diffB); // The following line is much faster, but more confusing to read //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; // Save the current color into the 'previous' buffer previousFrame[i] = currColor; } // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. if (movementSum > 0) { updatePixels(); // println(movementSum); // Print the total amount of movement to the console } } } void filter5(){ if (myCapture.available()) { // When using video to manipulate the screen, use video.available() and // video.read() inside the draw() method so that it's safe to draw to the screen myCapture.read(); // Read the new frame from the camera myCapture.loadPixels(); // Make its pixels[] array available int movementSum = 0; // Amount of movement in the frame for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame... color currColor = myCapture.pixels[i]; color prevColor = previousFrame[i]; // Extract the red, green, and blue components from current pixel int currR = (currColor >> 16) & 0xFF; // Like red(), but faster int currG = (currColor >> 8) & 0xFF; int currB = currColor & 0xFF; // Extract red, green, and blue components from previous pixel int prevR = (prevColor >> 16) & 0xFF; int prevG = (prevColor >> 8) & 0xFF; int prevB = prevColor & 0xFF; // Compute the difference of the red, green, and blue values int diffR = abs(currR - prevR); int diffG = abs(currG - prevG); int diffB = abs(currB - prevB); // Add these differences to the running tally movementSum += diffR + diffG + diffB; //render difference to grey if(((diffR + diffG + diffB) > 50)){ pixels[i] = color(int(random(255)), int(random(255)), int(random(255))); //if both are different, render red if(((diffR + diffG + diffB) > 150) && ((prevR + prevG + prevB) > 150)){ pixels[i] = color(int(random(255)), 0, 0); } } else { pixels[i] = color(255); } // Render the difference image to the screen //pixels[i] = color(diffR, diffG, diffB); // The following line is much faster, but more confusing to read //pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB; // Save the current color into the 'previous' buffer previousFrame[i] = currColor; } // To prevent flicker from frames that are all black (no movement), // only update the screen if the image has changed. if (movementSum > 0) { updatePixels(); // println(movementSum); // Print the total amount of movement to the console } } } void filter6(){ fill(0); rect(0,0,width,height); } void filter7(){ fill(255); rect(0,0,width,height); }