xxxxxxxxxx
var capture, dstimg;
var w = 320;
var h = 320;
function setup() {
createCanvas(windowWidth, windowHeight);
capture = createCapture(VIDEO); // this opens up the camera
pixelDensity(1); // turns off oversampling ("retina display")
capture.size(w, h); // this is in pixels
capture.hide(); // telling the browser to hide the camera so we can draw it ourselves
dstimg = createImage(w, h); // create an empty picture
}
function draw() {
background(0);
capture.loadPixels(); // uncompress the image to the computer's RAM and store in the pixel array
dstimg.loadPixels();
// process image here:
let howmanypixels = capture.pixels.length;
let w = capture.width;
let h = capture.height;
let saturation = map(mouseX, 0, width, -5, 5);
// spatial processing of an image:
// location variables
// step one: color channels:
let r = 0;
let g = 1;
let b = 2;
let a = 3;
let rr = 0; // output red
let gg = 0; // output green
let bb = 0; // output blue
// convolution kernel:
var k1 = [[-1.0, 0.0, 1.0],
[-2.0, 0.0, 2.0],
[-1.0, 0.0, 1.0]];
var k2 = [[-1.0, -2.0, -1.0],
[0.0, 0.0, 0.0],
[1.0, 2.0, 1.0]];
// var k1 = [[1.0, 0.0, 0.0],
// [0.0, -4.0, 0.0],
// [0.0, 0.0, 1.0]];
// var k2 = [[0.0, -1.0, 0.0],
// [-1.0, 1.0, -1.0],
// [1.0, -1.0, 0.0]];
for(let x = 0;x<w;x++)
{
for(let y = 0;y<h;y++)
{
// step two: neighbors
let ul = ((x-1+w)%w + w*((y-1+h)%h))*4; // location of the upperleft pixel
let uc = ((x+0+w)%w + w*((y-1+h)%h))*4; // location of the upper pixel
let ur = ((x+0+w)%w + w*((y+1+h)%h))*4; // location of the upperright pixel
let ml = ((x-1+w)%w + w*((y+0+h)%h))*4; // location of the left pixel
let mc = ((x+0+w)%w + w*((y+0+h)%h))*4; // location of the CENTER pixel
let mr = ((x+1+w)%w + w*((y+0+h)%h))*4; // location of the right pixel
let ll = ((x-1+w)%w + w*((y+1+h)%h))*4; // location of the lowerleft pixel
let lc = ((x+0+w)%w + w*((y+1+h)%h))*4; // location of the lower pixel
let lr = ((x+1+w)%w + w*((y+1+h)%h))*4; // location of the lowerright pixel
let pxul = 0.2126*capture.pixels[ul+r] + 0.7152*capture.pixels[ul+g] + 0.0722*capture.pixels[ul+b];
let pxuc = 0.2126*capture.pixels[uc+r] + 0.7152*capture.pixels[uc+g] + 0.0722*capture.pixels[uc+b];
let pxur = 0.2126*capture.pixels[ur+r] + 0.7152*capture.pixels[ur+g] + 0.0722*capture.pixels[ur+b];
let pxml = 0.2126*capture.pixels[ml+r] + 0.7152*capture.pixels[ml+g] + 0.0722*capture.pixels[ml+b];
let pxmc = 0.2126*capture.pixels[mc+r] + 0.7152*capture.pixels[mc+g] + 0.0722*capture.pixels[mc+b];
let pxmr = 0.2126*capture.pixels[mr+r] + 0.7152*capture.pixels[mr+g] + 0.0722*capture.pixels[mr+b];
let pxll = 0.2126*capture.pixels[ll+r] + 0.7152*capture.pixels[ll+g] + 0.0722*capture.pixels[ll+b];
let pxlc = 0.2126*capture.pixels[lc+r] + 0.7152*capture.pixels[lc+g] + 0.0722*capture.pixels[lc+b];
let pxlr = 0.2126*capture.pixels[lr+r] + 0.7152*capture.pixels[lr+g] + 0.0722*capture.pixels[lr+b];
let out1 = 0;
out1 += pxul*k1[0][0];
out1 += pxuc*k1[0][1];
out1 += pxur*k1[0][2];
out1 += pxml*k1[1][0];
out1 += pxmc*k1[1][1];
out1 += pxmr*k1[1][2];
out1 += pxll*k1[2][0];
out1 += pxlc*k1[2][1];
out1 += pxlr*k1[2][2];
let out2 = 0;
out2 += pxul*k2[0][0];
out2 += pxuc*k2[0][1];
out2 += pxur*k2[0][2];
out2 += pxml*k2[1][0];
out2 += pxmc*k2[1][1];
out2 += pxmr*k2[1][2];
out2 += pxll*k2[2][0];
out2 += pxlc*k2[2][1];
out2 += pxlr*k2[2][2];
let finalout = sqrt(out1*out1 + out2*out2);
dstimg.pixels[mc+r] = finalout;
dstimg.pixels[mc+g] = finalout;
dstimg.pixels[mc+b] = finalout;
dstimg.pixels[mc+a] = 255;
}
}
//capture.updatePixels(); // puts the modified pixel array back and xfers it to the GPU
dstimg.updatePixels();
noSmooth();
image(dstimg, 0, 0, width, height);
}