I am trying to do an assignment, and I have a piece of reference code that I am basing part of my assignment on which i have modified so that it fits my project, however, it is just one part of the project and when i try to incorporate it in with other parts of my code that use the opencv library, it wont work because it uses the procesing video library. I'm not sure how to change it...help please? :)
Code:
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import processing.video.*;
Capture cameraFrame;
PImage previousFrame;
PImage currentFrame;
PImage differenceFrame;
PImage backgroundImage;
float foregroundAlpha;
int maxDiff;
float thresholdValue;
Minim minim;
AudioPlayer au_playerMove1; //where are you going?
AudioPlayer au_playerMove2; //are you leaving?
AudioPlayer au_playerMove3; //come back!
void setup() {
size(320, 240);
cameraFrame = new Capture(this, width, height);
previousFrame = new PImage(width, height);
currentFrame = new PImage(width, height);
differenceFrame = new PImage(width, height);
minim = new Minim(this);
backgroundImage = new PImage(width, height);
maxDiff = (width*height)/10;
thresholdValue = 0.20;
au_playerMove1 = minim.loadFile("going.wav");
au_playerMove2 = minim.loadFile("leaving.wav");
au_playerMove3 = minim.loadFile("comeback.wav");
}
void draw() {
// Draw the display frame buffer to the sketch window
noTint();
image(backgroundImage, 0, 0);
image(currentFrame, 0, 0);
}
// This function is called when a new frame is ready from the camera
void captureEvent(Capture cameraFrame) {
// First we read the frame from the camera
cameraFrame.read();
// Next we store the current frame buffer into the previous frame buffer
previousFrame.copy(currentFrame, 0, 0, width, height, 0, 0, width, height);
// Now we store the frame from the camera into the current frame buffer
currentFrame.copy(cameraFrame, 0, 0, width, height, 0, 0, width, height);
// Copy the previous frame to the difference frame
differenceFrame.copy(previousFrame, 0, 0, width, height, 0, 0, width, height);
// Blend the current frame with the previous frame using the DIFFERENCE function
differenceFrame.blend(currentFrame, 0, 0, width, height, 0, 0, width, height, DIFFERENCE);
differenceFrame.filter(THRESHOLD, thresholdValue);
// If a pixel in the difference pixel is white (255) it's different from the previous frame and we want to count it
// countPixels is the function below: it counts and returns all pixels of a given colour (the colour that is passed to it as a parameter)
int differenceCount = countPixels(differenceFrame, color(255));
if (differenceCount >=10000 && differenceCount <=15000) {
println ("movement");
au_playerMove1.play();
} if (differenceCount >=15001 && differenceCount <=20000) {
au_playerMove2.play();
} if (differenceCount >=20001 && differenceCount <=25000) {
au_playerMove3.play();
}
// Calculate an alpha value based on number of pixels that are different
// Check setup function for the value of maxDiff, print out differencecount to see number of different pixels between two frames
// println(differenceCount);
// The map function maps the value from differenceCount, expected to be within the range 0-maxDiff, to a value within the range 0-255 (alpha).
float differenceAlpha = map(differenceCount, 0, maxDiff, 0, 255);
// Update the foreground alpha value gradually to avoid flicker
foregroundAlpha += 0.1 * (differenceAlpha - foregroundAlpha);
}
/*void keyPressed() {
if(key==' ') {
backgroundImage.copy(currentFrame, 0, 0, width, height, 0, 0, width, height);
}
}*/
int countPixels(PImage img, color c) {
// Call loadPixels so that we can access the individual pixel values
img.loadPixels();
int pixelCount = 0;
for (int i = 0; i < img.pixels.length; i++) {
if (img.pixels[i] == c) pixelCount++;
}
return pixelCount;
}
Code:
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import processing.video.*;
Capture cameraFrame;
PImage previousFrame;
PImage currentFrame;
PImage differenceFrame;
PImage backgroundImage;
float foregroundAlpha;
int maxDiff;
float thresholdValue;
Minim minim;
AudioPlayer au_playerMove1; //where are you going?
AudioPlayer au_playerMove2; //are you leaving?
AudioPlayer au_playerMove3; //come back!
void setup() {
size(320, 240);
cameraFrame = new Capture(this, width, height);
previousFrame = new PImage(width, height);
currentFrame = new PImage(width, height);
differenceFrame = new PImage(width, height);
minim = new Minim(this);
backgroundImage = new PImage(width, height);
maxDiff = (width*height)/10;
thresholdValue = 0.20;
au_playerMove1 = minim.loadFile("going.wav");
au_playerMove2 = minim.loadFile("leaving.wav");
au_playerMove3 = minim.loadFile("comeback.wav");
}
void draw() {
// Draw the display frame buffer to the sketch window
noTint();
image(backgroundImage, 0, 0);
image(currentFrame, 0, 0);
}
// This function is called when a new frame is ready from the camera
void captureEvent(Capture cameraFrame) {
// First we read the frame from the camera
cameraFrame.read();
// Next we store the current frame buffer into the previous frame buffer
previousFrame.copy(currentFrame, 0, 0, width, height, 0, 0, width, height);
// Now we store the frame from the camera into the current frame buffer
currentFrame.copy(cameraFrame, 0, 0, width, height, 0, 0, width, height);
// Copy the previous frame to the difference frame
differenceFrame.copy(previousFrame, 0, 0, width, height, 0, 0, width, height);
// Blend the current frame with the previous frame using the DIFFERENCE function
differenceFrame.blend(currentFrame, 0, 0, width, height, 0, 0, width, height, DIFFERENCE);
differenceFrame.filter(THRESHOLD, thresholdValue);
// If a pixel in the difference pixel is white (255) it's different from the previous frame and we want to count it
// countPixels is the function below: it counts and returns all pixels of a given colour (the colour that is passed to it as a parameter)
int differenceCount = countPixels(differenceFrame, color(255));
if (differenceCount >=10000 && differenceCount <=15000) {
println ("movement");
au_playerMove1.play();
} if (differenceCount >=15001 && differenceCount <=20000) {
au_playerMove2.play();
} if (differenceCount >=20001 && differenceCount <=25000) {
au_playerMove3.play();
}
// Calculate an alpha value based on number of pixels that are different
// Check setup function for the value of maxDiff, print out differencecount to see number of different pixels between two frames
// println(differenceCount);
// The map function maps the value from differenceCount, expected to be within the range 0-maxDiff, to a value within the range 0-255 (alpha).
float differenceAlpha = map(differenceCount, 0, maxDiff, 0, 255);
// Update the foreground alpha value gradually to avoid flicker
foregroundAlpha += 0.1 * (differenceAlpha - foregroundAlpha);
}
/*void keyPressed() {
if(key==' ') {
backgroundImage.copy(currentFrame, 0, 0, width, height, 0, 0, width, height);
}
}*/
int countPixels(PImage img, color c) {
// Call loadPixels so that we can access the individual pixel values
img.loadPixels();
int pixelCount = 0;
for (int i = 0; i < img.pixels.length; i++) {
if (img.pixels[i] == c) pixelCount++;
}
return pixelCount;
}
1