Monday, December 5, 2011

Embedded Media Processing Code

Here is my source code for "Interactive Instructionals" final project:

*Both of these have not resolved the issue of using movie.play/pause as a controller for the video action.


This version would eventually rely on a lightsource behind the user to cast a shadow onto the screen:
import processing.video.*;
int numPixels;
int[] backgroundPixels;
PImage pCam; // camera source RAW
PImage finalImage; // background subtracted Camera
int dispNum;
float camThreshold = 0.09;
float moviePosition = 0;
float increment = .5;

Capture cam;
Movie myMovie;
Movie proj;
int presenceSum;

int threshold = 34;

void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(640, 480);

// Setting up container for Camera Image

// CAMERA
String[] cameras = Capture.list();

if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
}
else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}

cam = new Capture(this, 640, 480, cameras[0]);
cam.start();
}
numPixels = cam.width * cam.height;
// Create array to store the background image
pCam = createImage(640, 480, ARGB);
finalImage = createImage(640, 480, ARGB);
backgroundPixels = new int[numPixels];

loadPixels();

// VIDEO
myMovie = new Movie(this, "souljawht.mov");
myMovie.loop();
proj = new Movie(this, "proj2.mov");
proj.loop();

}

void draw() {
// background(255);
if (cam.available()) {
cam.read(); // Read a new cam frame
cam.loadPixels(); // Make the pixels of cam available
// Difference between the current frame and the stored background
// int presenceSum = 0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the cam frame...
// Fetch the current color in that location, and also the color
// of the background in that spot
color currColor = cam.pixels[i];
color bkgdColor = backgroundPixels[i];
// Extract the red, green, and blue components of the current pixel�s color
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
int grayScaleCamera = (currR + currG + currB) / 3; // Camera image in Grayscale
// Extract the red, green, and blue components of the background pixel�s color
int bkgdR = (bkgdColor >> 16) & 0xFF;
int bkgdG = (bkgdColor >> 8) & 0xFF;
int bkgdB = bkgdColor & 0xFF;
int grayScaleBG = (bkgdR + bkgdG + bkgdB) / 3; // Background Buffer in grayscale
// Compute the difference of the red, green, and blue values
int diffR = abs(currR - bkgdR);
int diffG = abs(currG - bkgdG);
int diffB = abs(currB - bkgdB);
// int diffGray = abs(grayScaleCamera - grayScaleBG);


pCam.pixels[i] = color (diffR, diffG, diffB);
// if (pCam > threshold) {
// pCam = 0;
// }
// else {
// pCam = 255;
// }

// updatePixels(); // Notify that the pixels[] array has changed
}

pCam.updatePixels();
pCam.filter(THRESHOLD, camThreshold);


if (myMovie.available()) {
myMovie.read();
myMovie.loadPixels(); // Make the pixels of video available
image(myMovie, 0, 0);
}
if (proj.available()) {
proj.read();
proj.loadPixels(); // Make the pixels of video available
image(proj, 0, 0);
}
presenceSum = 0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the cam frame...
finalImage.pixels[i] = color(abs(red(pCam.pixels[i]) - red(myMovie.pixels[i])), abs(green(pCam.pixels[i]) - green(myMovie.pixels[i])), abs(blue(pCam.pixels[i]) - blue(myMovie.pixels[i])));
finalImage.updatePixels();
presenceSum += (red(finalImage.pixels[i])+ green(finalImage.pixels[i]) + blue(finalImage.pixels[i]))/3/255;
}
println(presenceSum);
}//myMovie
if (presenceSum <= 17000){
myMovie.play();
proj.play();
} else {
myMovie.pause();
proj.pause();
}

if (dispNum == 1) {
image(pCam, 0, 0);
}
else if (dispNum == 2) {
image(myMovie, 0, 0);
}
else if (dispNum == 3) {
image(finalImage, 0, 0);
}
else if (dispNum == 4) {
image(proj, 0, 0);
}
}


// When a key is pressed, capture the background image into the backgroundPixels
// buffer, by copying each of the current frame�s pixels into it.
void keyPressed() {
if (keyCode == UP) {
camThreshold += .01 ;
println (camThreshold);
}

if (keyCode == DOWN) {
camThreshold -= .01 ;
println (camThreshold);
}
if (key == ' ') {
cam.loadPixels();
arraycopy(cam.pixels, backgroundPixels);
}
if (key == 'o') {
myMovie.frameRate(4);
}
if (key == 'p') {
myMovie.frameRate(30);
}
if (key == 'q') {
myMovie.pause();
}
if (key == 'j') {
}
if (key == 'w') {
myMovie.play();
}
if (key == '1') {
dispNum = 1;
}
if (key == '2') {
dispNum = 2;
}
if (key == '3') {
dispNum = 3;
}
if (key == '4') {
dispNum = 4;
}

}


This version overlays (or rather underlays) the "pCam" to give the user a reference while still viewing the canned video
import processing.video.*;
int numPixels;
int[] backgroundPixels;
PImage pCam; // camera source RAW
PImage finalImage; // background subtracted Camera
int dispNum;
float camThreshold = 0.09;
float moviePosition = 0;
float increment = .5;

Capture cam;
Movie myMovie;
Movie proj;
int presenceSum;

int threshold = 34;

void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(640, 480);

// Setting up container for Camera Image

// CAMERA
String[] cameras = Capture.list();

if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
}
else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}

cam = new Capture(this, 640, 480, cameras[0]);
cam.start();
}
numPixels = cam.width * cam.height;
// Create array to store the background image
pCam = createImage(640, 480, ARGB);
finalImage = createImage(640, 480, ARGB);
backgroundPixels = new int[numPixels];

loadPixels();

// VIDEO
myMovie = new Movie(this, "souljawht.mov");
myMovie.loop();
proj = new Movie(this, "proj2.mov");
proj.loop();

}

void draw() {
// background(255);
if (cam.available()) {
cam.read(); // Read a new cam frame
cam.loadPixels(); // Make the pixels of cam available
// Difference between the current frame and the stored background
// int presenceSum = 0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the cam frame...
// Fetch the current color in that location, and also the color
// of the background in that spot
color currColor = cam.pixels[i];
color bkgdColor = backgroundPixels[i];
// Extract the red, green, and blue components of the current pixel�s color
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
int grayScaleCamera = (currR + currG + currB) / 3; // Camera image in Grayscale
// Extract the red, green, and blue components of the background pixel�s color
int bkgdR = (bkgdColor >> 16) & 0xFF;
int bkgdG = (bkgdColor >> 8) & 0xFF;
int bkgdB = bkgdColor & 0xFF;
int grayScaleBG = (bkgdR + bkgdG + bkgdB) / 3; // Background Buffer in grayscale
// Compute the difference of the red, green, and blue values
int diffR = abs(currR - bkgdR);
int diffG = abs(currG - bkgdG);
int diffB = abs(currB - bkgdB);
// int diffGray = abs(grayScaleCamera - grayScaleBG);


pCam.pixels[i] = color (diffR, diffG, diffB);
// if (pCam > threshold) {
// pCam = 0;
// }
// else {
// pCam = 255;
// }

// updatePixels(); // Notify that the pixels[] array has changed
}

pCam.updatePixels();
pCam.filter(THRESHOLD, camThreshold);


if (myMovie.available()) {
myMovie.read();
myMovie.loadPixels(); // Make the pixels of video available
image(myMovie, 0, 0);
}
if (proj.available()) {
proj.read();
proj.loadPixels(); // Make the pixels of video available
image(proj, 0, 0);
}
presenceSum = 0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the cam frame...
finalImage.pixels[i] = color(abs(red(pCam.pixels[i]) - red(myMovie.pixels[i])), abs(green(pCam.pixels[i]) - green(myMovie.pixels[i])), abs(blue(pCam.pixels[i]) - blue(myMovie.pixels[i])));
finalImage.updatePixels();
presenceSum += (red(finalImage.pixels[i])+ green(finalImage.pixels[i]) + blue(finalImage.pixels[i]))/3/255;
}
println(presenceSum);
}//myMovie
if (presenceSum <= 17000){
myMovie.play();
proj.play();
} else {
myMovie.pause();
proj.pause();
}

if (dispNum == 1) {
image(pCam, 0, 0);
}
else if (dispNum == 2) {
image(myMovie, 0, 0);
}
else if (dispNum == 3) {
image(finalImage, 0, 0);
}
else if (dispNum == 4) {
image(proj, 0, 0);
}
}


// When a key is pressed, capture the background image into the backgroundPixels
// buffer, by copying each of the current frame�s pixels into it.
void keyPressed() {
if (keyCode == UP) {
camThreshold += .01 ;
println (camThreshold);
}

if (keyCode == DOWN) {
camThreshold -= .01 ;
println (camThreshold);
}
if (key == ' ') {
cam.loadPixels();
arraycopy(cam.pixels, backgroundPixels);
}
if (key == 'o') {
myMovie.frameRate(4);
}
if (key == 'p') {
myMovie.frameRate(30);
}
if (key == 'q') {
myMovie.pause();
}
if (key == 'j') {
}
if (key == 'w') {
myMovie.play();
}
if (key == '1') {
dispNum = 1;
}
if (key == '2') {
dispNum = 2;
}
if (key == '3') {
dispNum = 3;
}
if (key == '4') {
dispNum = 4;
}

}

No comments:

Post a Comment