xxxxxxxxxx
// computer vision test
// hipster burglar alarm
var capture, previous, diff;
var carray = new Array(32); // centroid array
var thresh = 30;
function preload() {
}
function setup() {
createCanvas(windowWidth, windowHeight);
capture = createCapture(VIDEO); // this opens up the camera
pixelDensity(1); // turns off oversampling ("retina display")
capture.size(32, 32); // this is in pixels
capture.hide(); // telling the browser to hide the camera so we can draw it ourselves
previous = createImage(32, 32); // create an empty picture
diff = createImage(32, 32); // create an empty picture
for(let i = 0;i<carray.length;i++)
{
carray[i] = new Array(32);
for(let j = 0;j<carray[i].length;j++)
{
carray[i][j] = new Array(2);
}
}
}
function draw() {
background(0);
capture.loadPixels();
previous.loadPixels();
diff.loadPixels();
noSmooth();
var x, y;
for(let i = 0;i<capture.pixels.length;i+=4)
{
let avg = (abs(capture.pixels[i]-previous.pixels[i]) + abs(capture.pixels[i+1]-previous.pixels[i+1]) + abs(capture.pixels[i+2]-previous.pixels[i+2]))/3;
diff.pixels[i]=(avg>thresh)*255;
diff.pixels[i+1]=(avg>thresh)*255;
diff.pixels[i+2]=(avg>thresh)*255;
diff.pixels[i+3]=255;
// x = floor(i/4)%capture.pixels.width;
// y = floor((i/4)/capture.pixels.width);
x = 10;
y = 10;
if(avg>thresh)
{
carray[x][y][0] = x;
carray[x][y][1] = y;
}
else {
carray[x][y][0] = 0;
carray[x][y][1] = 0;
}
}
diff.updatePixels();
for(let i = 0;i<capture.pixels.length;i++)
{
previous.pixels[i] = capture.pixels[i];
}
previous.updatePixels();
image(capture, 0, 0, width/2, height/2);
image(diff, width/2, 0, width/2, height/2);
textSize(80);
fill(255);
// text(s[0], width/2, height/2 + 80);
// text(s[1], width/2, height/2 + 160);
// text(s[2], width/2, height/2 + 240);
// text(s[3], width/2, height/2 + 320);
}