We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys, I'm new to processing. I'm creating this project where upon detecting motion through the laptop camera, a sound is activate. I have the video and sound working, but I cannot seem to figure out my "if" statement. Any help is greatly appreciated. Thanks.
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import processing.video.*;
Capture video;
//Previous Frame
PImage prevFrame;
Minim minim;
AudioPlayer player;
// How different must a pixel be to be a "motion" pixel
float threshold = 100;
void setup(){
size(640, 480);
video = new Capture(this, width, height, 24);
prevFrame = createImage(video.width,video.height,RGB);
loadPixels();
video.start();
minim = new Minim (this);
player = minim.loadFile("UrBeautiful.mp3");
}
void draw(){
background(0);
image(video,0,0);
if (video. available()){
prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height);
prevFrame.updatePixels();
video.read();
// player.play();
}
loadPixels();
video.loadPixels();
prevFrame.loadPixels();
for (int i = 0; i < video.pixels.length; i ++ ) {
// Step 2, what is the current color
color current = video.pixels[i];
// Step 3, what is the previous color
color previous = prevFrame.pixels[i];
// Step 4, compare colors (previous vs. current)
float r1 = red(current);
float g1 = green(current);
float b1 = blue(current);
float r2 = red(previous);
float g2 = green(previous);
float b2 = blue(previous);
// Motion for an individual pixel is the difference between the previous color and current color.
float diff = dist(r1,g1,b1,r2,g2,b2);
if (diff > threshold){
pixels[i] = video.pixels[i];
player.play();
} else {
pixels[i] = video.pixels[i];
}
}
updatePixels();
}
Answers
I honestly don't think the color difference will surpass 100, but I'm not saying to lower the threshold (you're welcome to try, though).
I can't get the code to work on my computer, so I'll help you out as best as I can. I would replace the if(diff > threshold) with if(abs(r1-r2) || abs(g1-g2) || abs(b1 - b2) > sensitivity). Replace the threshold variable with float sensitivity.
I know this line of code is a bit long, but what it's doing is comparing the difference between the previous colors to the current ones, then checking if the difference is bigger than the sensitivity. Lower number equals higher sensitivity.
Again I have no guarantees that this will work, but hope that might mork.
any luck with this sketch ?i would like to try thanks, im looking to learn what the draw section does so that i can maybe use the technics used to get the final outcome