Processing Forum
PD: sorry about that quote, don't know how to remove it
- PImage img;
int w = 160;
// It's possible to perform a convolution
// the image with different matrices
float[][] V1matrix = { { -1, 0, 1 },
{ -2, 0, 2 },
{ -1, 0, 1 } };
float[][] V2matrix = { { 1, 0, -1 },
{ 2, 0, -2 },
{ 1, 0, -1 } };
float[][] H1matrix = { { -1, -2, -1 },
{ 0, 0, 0 },
{ 1, 2, 1 } };
float[][] H2matrix = { { 1, 2, 1 },
{ 0, 0, 0 },
{ -1, -2, -1 } };
void setup() {
size(500, 500);
frameRate(30);
img = loadImage("clock.jpg");
}
void draw() {
// We're only going to process a portion of the image
// so let's set the whole image as the background first
image(img,0,0);
// Where is the small rectangle we will process
int xstart = constrain(mouseX-w/2,0,img.width);
int ystart = constrain(mouseY-w/2,0,img.height);
int xend = constrain(mouseX+w/2,0,img.width);
int yend = constrain(mouseY+w/2,0,img.height);
int matrixsize = 3;
loadPixels();
// Begin our loop for every pixel
for (int x = xstart; x < xend; x++) {
for (int y = ystart; y < yend; y++ ) {
// Each pixel location (x,y) gets passed into a function called convolution()
// which returns a new color value to be displayed.
color c = convolution(x,y,V1matrix,V2matrix,H1matrix,H2matrix,matrixsize,img);
int loc = x + y*img.width;
pixels[loc] = c;
}
}
int mouseLoc = mouseX+mouseY*width;
println("(x="+mouseX+"|y="+mouseY+")==> color="+pixels[mouseLoc]);
updatePixels();
}
color convolution(int x, int y, float[][] V1matrix, float[][] V2matrix,float[][] H1matrix,float[][] H2matrix, int matrixsize, PImage img) {
float r1total = 0.0;
float g1total = 0.0;
float b1total = 0.0;
float r2total = 0.0;
float g2total = 0.0;
float b2total = 0.0;
int offset = matrixsize / 2;
// Loop through convolution matrix
for (int i = 0; i < matrixsize; i++){
for (int j= 0; j < matrixsize; j++){
// What pixel are we testing
int xloc = x+i-offset;
int yloc = y+j-offset;
int loc = xloc + img.width*yloc;
// Make sure we have not walked off the edge of the pixel array
loc = constrain(loc,0,img.pixels.length-1);
// Calculate the convolution
// We sum all the neighboring pixels multiplied by the values in the convolution matrix.
r1total += (red(img.pixels[loc]) * V1matrix[i][j]);
g1total += (green(img.pixels[loc]) * V1matrix[i][j]);
b1total += (blue(img.pixels[loc]) * V1matrix[i][j]);
r2total += (red(img.pixels[loc]) * V2matrix[i][j]);
g2total += (green(img.pixels[loc]) * V2matrix[i][j]);
b2total += (blue(img.pixels[loc]) * V2matrix[i][j]);
r1total += (red(img.pixels[loc]) * H1matrix[i][j]);
g1total += (green(img.pixels[loc]) * H1matrix[i][j]);
b1total += (blue(img.pixels[loc]) * H1matrix[i][j]);
r2total += (red(img.pixels[loc]) * H2matrix[i][j]);
g2total += (green(img.pixels[loc]) * H2matrix[i][j]);
b2total += (blue(img.pixels[loc]) * H2matrix[i][j]);
}
}
// Make sure RGB is within range (this is the part that won't work)
r1total = constrain(r1total, 0, 255);
g1total = constrain(g1total, 0, 255);
b1total = constrain(b1total, 0, 255);
r2total = constrain(r2total, 0, 255);
g2total = constrain(g2total, 0, 255);
b2total = constrain(b2total, 0, 255);
// Return the resulting color, choose 1 or 2
return color(r1total,g1total,b1total);
}