Hello, i was wondering how it would be possible to replace the get(x,y,w,h) function with something else to enhance performance… i’m currently working on a project where i have to “degrade the quality” of a webcam image in real time. I settled on an effect like this here. i integrated the code as is and it works but it slows down the whole sketch and the webcam feedback becomes so slow it’s as if it freezed… i believe it’s because of this command set(x2, y2, get(x1,y1,w,h));
, specifically the get() function?
I read that another way to access pixels faster is by directly going after the pixel array like this
myVideo.pixels[i], //r
myVideo.pixels[i + 1], //g
myVideo.pixels[i + 2], //b
myVideo.pixels[i + 3], //a
but i’m already confused at how i’m supposed to get the position or change the scale.
so far this is the code i have in draw
myVideo.loadPixels();
for (let y = 0; y < myVideo.height; y ++) {
for (let x = 0; x < myVideo.width; x ++) {
var r = getQuick(myVideo, x, y)[0];
var g = getQuick(myVideo, x, y)[1];
var b = getQuick(myVideo, x, y)[2];
var a = getQuick(myVideo, x, y)[3];
if (r < effect || g < effect || b < effect || a < effect ) {
var x1 = random(myVideo.width);
var y1 = random(myVideo.height);
var x2 = round(x1 + random(-10, 10));
var y2 = round(y1 + random(-10, 10));
var w = 10;
var h = 10;
set(x2, y2, get(x1,y1,w,h));
}
}
}
myVideo.updatePixels();
Can someone help me solve this? I found that using the pixel array to simply manipulate the color of the pixels worked very well and allowed speed and fluidity. is it possible to do the same for position and scale and get a result as shown in this example? i already took a look at the references for set(), get(), and pixels[] but it did not help me understand how i’m supposed to execute what im trying to do
by the way, i created this function though i am not sure it is entirely necessary, but anyway, putting this here in case i am actually making my life harder with useless stuff
function getQuick(myVideo, x, y) {
var i = (y * myVideo.width + x) * 4;
return [
myVideo.pixels[i], //r
myVideo.pixels[i + 1], //g
myVideo.pixels[i + 2], //b
myVideo.pixels[i + 3], //a
];
}