xxxxxxxxxx
75
let capture;
let previousPixels;
function setup() {
createCanvas(640, 360);
capture = createCapture(VIDEO);
capture.size(640, 360);
capture.hide();
pixelDensity(1);
noStroke();
}
function copyImage(src, dst) {
let n = src.length;
if (!dst || dst.length != n) dst = new src.constructor(n);
while (n--) dst[n] = src[n];
return dst;
}
function draw() {
capture.loadPixels();
let total = 0;
if (capture.pixels.length > 0) {
// don't forget this!
if (!previousPixels) {
previousPixels = copyImage(capture.pixels, previousPixels);
} else
{
let thresholdAmount = 50;
thresholdAmount *= 3; // 3 for r, g, b
for (let y = 0; y < capture.height; y++) {
for (let x = 0; x < capture.width; x++) {
let index = 4 * (x + y * width);
// calculate the differences
let rdiff = Math.abs(
capture.pixels[index + 0] - previousPixels[index + 0]
);
let gdiff = Math.abs(
capture.pixels[index + 1] - previousPixels[index + 1]
);
let bdiff = Math.abs(
capture.pixels[index + 2] - previousPixels[index + 2]
);
// copy the current pixels to previousPixels
previousPixels[index + 0] = capture.pixels[index + 0];
previousPixels[index + 1] = capture.pixels[index + 1];
previousPixels[index + 2] = capture.pixels[index + 2];
let diffs = rdiff + gdiff + bdiff;
let output = 0;
if (diffs > thresholdAmount) {
output = 255;
total += diffs;
}
capture.pixels[index] = output;
capture.pixels[index + 1] = output;
capture.pixels[index + 2] = output;
// also try this:
// capture.pixels[i++] = rdiff;
// capture.pixels[i++] = gdiff;
// capture.pixels[i++] = bdiff;
}
}
}
}
// need this because sometimes the frames are repeated
if (total > 0) {
capture.updatePixels();
image(capture, 0, 0, 640, 480);
}
let avgMotion=total/capture.pixels.length;
fill(255);
rect(0,height-avgMotion*20,100,height);
}