Using multiple webcams to build a panorama used for motion detection
-
/*
* developed by John Flitcroft and Serena Leonardi from movement code
* origonally by andrewwright
*
*/
import processing.video.*; //Library
import ddf.minim.*;
int W = 640; //Hight
int H = 480; //Width
Minim minim;
AudioOutput out;
AudioSample Bong;
AudioSample Bim;
color pColor;
Capture Rcam;
Capture Lcam;
PImage Panorama;
int res = 60; //Resolution (Higher=Bigger pixels)
int thresh = 50; //Threshold (Higher, less accurate/ bigger differances needed)
color[] buffer;
void setup()
{
size ( W * 2, H );
background(0);
Rcam = new Capture(this, W, H);
Rcam.settings();
Lcam = new Capture(this, W, H);
Lcam.settings();
Panorama = new PImage(W * 2, H);
buffer = new color[W * 2 * H];
minim = new Minim(this);
Bong = minim.loadSample("Bong.mp3", 2048);
Bim = minim.loadSample("Bim.mp3", 2048);
}
void captureEvent(Capture Panorama)
{
Panorama.read();
}
void draw() //Draw Image from buffer, in order to compaire for movement.
{
video();
sampleAndPaint1();
sampleAndPaint2();
swapBuffer();
}
void video()
{
if(Lcam.available());
{
Lcam.read();
Lcam.loadPixels();
Panorama.copy(Lcam, 0, 0, W, H, 0, 0, W, H);
}
if(Rcam.available());
{
Rcam.read();
Rcam.loadPixels();
Panorama.copy(Rcam, 0, 0, W, H, W, 0, W, H);
}
}
void sampleAndPaint1() //Top Left
{
fill(0, 10); //How long presence lingers
rect(0, 0, W, H); //Create rectangle (location for motion detection)
for ( int i = 0; i < W; i+= res )
{
for ( int j = 0; j < H; j+=res ) //j < Hight of y, 240= half way
{
int offset = i + j * W;
color c = Panorama.pixels[offset];
color r = buffer[offset];
float cB = brightness(c);
float rB = brightness(r);
float delta = abs ( cB - rB );
if ( delta > thresh )// if differance is greater than threshold fill with...
{
fill ( 0, 255, 0 ); // fill empty pixels with.. RGB
noStroke();
rect ( i, j, res, res ); //create new image, output
Bong.trigger();
}
}
}
}
void sampleAndPaint2() //Top Middle
{
fill(0, 10); //How long presence lingers
rect(W, 0, W, H); //Create rectangle (location for motion detection)
for ( int i = W; i < W * 2; i+= res ) // i =x, x is < than 32... must be location
{
for ( int j = 0; j < H; j+=res )//j < Hight of y, 240= half way
{
int offset = i + j * W;
color c = Panorama.pixels[offset];
color r = buffer[offset];
float cB = brightness(c);
float rB = brightness(r);
float delta = abs ( cB - rB );
if ( delta > thresh )// if differance is greater than threshold fill with...
{
fill ( 255, 0, 0 ); // fill empty pixels with.. RGB
noStroke();
rect ( i, j, res, res ); //create new image, output
Bim.trigger();
}
}
}
}
void swapBuffer()
{
arrayCopy(Panorama.pixels, buffer); // Save frame, to compare for movement
}