We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys, This is my code so far. I'm supposed to blur a basketball so it looks like it has motion. However, I have no idea where the error in my code is and how I'm supposed to fix it. When I run the code, it just show a blank white screen. Thanks!
int x = 0;
int y = 0;
PImage img;
float[][] blur = { { 0, 0, 0 } ,
{ 1.0/3.0, 1.0/3.0, 1.0/3.0 } ,
{ 0, 0, 0 } } ;
//float [][] gFilter = matrix;
void setup() {
size(600,600);
background(255);
img = loadImage("BasketballBorder.png");
frameRate(60);
}
void draw() {
image(img, x, 0);
float [][] gFilter = blur;
int matrixsize = gFilter.length;
loadPixels();
//Code
for (int x = 0; x<img.width; x++) {
for (int y = 0; y<img.width; y++) {
color c = convolution(x,y,blur,matrixsize,img);
int loc = x + y*img.width;
pixels[loc] = c;
}
}
updatePixels();
}
color convolution(int x, int y, float[][] matrix, int matrixsize, PImage img)
{
float rtotal=0.0;
float gtotal=0.0;
float btotal=0.0;
int offset=matrixsize/2;
for (int i=0; i<matrixsize; i++) {
for (int j=0; j<matrixsize; j++) {
int xloc = x + i-offset;
int yloc = y + j-offset;
int loc = xloc + img.width*yloc;
loc = constrain(loc, 0, img.pixels.length-1);
rtotal += (red(img.pixels[loc]) * matrix[i][j]); //<<<<<This is where processing points out the error in my code
gtotal += (green(img.pixels[loc]) * matrix[i][j]);
btotal += (blue(img.pixels[loc]) * matrix[i][j]);
}
}
// Make sure RGB is within range
rtotal = constrain(rtotal, 0, 255);
gtotal = constrain(gtotal, 0, 255);
btotal = constrain(btotal, 0, 255);
// Return the resulting color
return color(rtotal, gtotal, btotal);
}
Answers
In order to manipulate your image's pixels[] array you have to call your image's loadPixels()/updatePixels. corrected your code and commented the incorrect lines:
And for anyone who is interested a fast shader based version:
And the dirBlur.frag (should be in your project's data folder):