/** * Frame Differencing * by Golan Levin. * * Quantify the amount of movement in the video frame using frame-differencing. */ // modded significantly by Les Hall // this comment added Fri Feb 19 2016 // again modifie by Les Hall // Thu May 26 2016, 5:20 pm // added sound and osc // works with The Brick 7.ck import processing.video.*; import processing.sound.*; SinOsc sineL; SinOsc sineR; int toggleAudio = 0; import oscP5.*; import netP5.*; OscP5 oscP5; NetAddress myRemoteLocation; int alpha = 4; int indexMax = 32; int numPixels; int[] previousFrame; Capture video; int index = 0; int xMax = 640*2/alpha; int yMax = 480*2/alpha; int iMax = xMax * yMax; int[][] frameBuffer = new int[indexMax][iMax]; int[] frame = new int[iMax]; void setup() { size(1280, 960); // This the default video input, see the GettingStartedCapture // example if it creates an error video = new Capture(this, xMax, yMax); // Start capturing the images from the camera video.start(); numPixels = video.width * video.height; // Create an array to store the previously captured frame previousFrame = new int[numPixels]; loadPixels(); // Create the sine oscillators. sineL = new SinOsc(this); sineL.play(); sineL.pan(-1); sineR = new SinOsc(this); sineR.play(); sineR.pan(1); frameRate(25); /* start oscP5, listening for incoming messages at port 12000 */ oscP5 = new OscP5(this,12000); /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters, * an ip address and a port number. myRemoteLocation is used as parameter in * oscP5.send() when sending osc packets to another computer, device, * application. usage see below. for testing purposes the listening port * and the port of the remote location address are the same, hence you will * send messages back to this sketch. */ myRemoteLocation = new NetAddress("127.0.0.1",12000); } void draw() { if (video.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 video.read(); // Read the new frame from the camera video.loadPixels(); // Make its pixels[] array available int movementSum = 0; // Amount of movement in the frame int movementColumnSumL = 0; // Aount of movement in each left screen column int movementColumnSumR = 0; // Aount of movement in each right screen column int movementColumnSumU = 0; // Aount of movement in each screen row for (int x = 0; x < xMax; ++x) { for (int y = 0; y < yMax; ++y) { // calculate indices int i = xMax*y + x; // read index int j = xMax*(y+1) - x - 1; // write index (inverted horizontally) color currColor = video.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 float diff = diffR + diffG + diffB; movementSum += diff; if (x < (xMax/2)) movementColumnSumL += diff * (xMax/2-x); else movementColumnSumR += diff * (x-xMax/2); movementColumnSumU += diff * (yMax-y); // store the image in the frame buffer frameBuffer[index][j] = color(diffR, diffG, diffB); // Render the difference image to the screen frame[j] = 0; for (int k=0; k 0) { updatePixels(); float numL = (movementColumnSumL/100000000.0); float numR = (movementColumnSumR/100000000.0); float numU = (movementColumnSumU/100000000.0); sineL.freq(50.0*numL); sineL.amp(toggleAudio * numU/10.0); sineR.freq(50.0*numR); sineR.amp(toggleAudio * numU/10.0); /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage("/exercise"); myMessage.add(numL); /* add a float to the osc message */ myMessage.add(numR); /* add a float to the osc message */ myMessage.add(numU); /* add a float to the osc message */ /* send the message */ oscP5.send(myMessage, myRemoteLocation); println(movementColumnSumL); // Print the total amount of movement to the console } // update the write index index = (index + 1) % indexMax; } } void mousePressed() { toggleAudio = 1-toggleAudio; } float sigmoid(float k, float x) { return (1.0/(k + exp(-x))); }