Problem with images example -- potentially local/global variables?
in
Programming Questions
•
1 year ago
Hey all,
I'm trying to modify some of the image examples (
http://processing.org/learning/pixels/) to do some photo editing algorithms.
The first thing I tried was a blur algorithm:
- //-----------------Globals
- PImage target;
- PImage destination;
- float v = 1.0/9.0;
- float[][] kernel = {{v,v,v},{v,v,v},{v,v,v}};
- //-----------------Setup
- void setup() {
- size(1024,683);
- target = loadImage("data/leaf.jpg");
- destination = createImage(target.width, target.height, RGB);
- target.loadPixels();
- destination.loadPixels();
- //Iterate over pixels
- for (int x = 1; x < target.width - 1; x++ ) {
- for (int y = 1; y < target.height - 1; y++ ) {
- color blurColor = blur(kernel,3,target);
- destination.pixels[y*target.width + x] = blurColor;
- }
- }
- //Update the pixels in the destination file, show the image, and save it.
- destination.updatePixels();
- image(destination,0,0);
- image(target,512,0);
- //save("leaf_blur.png");
- }
- //-----------------Defined Functions
- color blur(float[][] matrix, int matrixSize, PImage img) {
- float redSum = 0;
- float greenSum = 0;
- float blueSum = 0;
- for (int dx = -(matrixSize - 1) / 2; dx <= (matrixSize - 1) / 2; dx++) {
- for (int dy = -(matrixSize - 1) / 2; dy <= (matrixSize - 1) / 2; dy++) {
- //snag the location of the pixel
- int loc = (x+dx) + (y+dy)*img.width;
- // Pick a parameter of the pixel
- float redVal = red(img.pixels[loc]);
- float greenVal = green(img.pixels[loc]);
- float blueVal = blue(img.pixels[loc]);
- // Multiply adjacent pixels based on the kernel values
- redSum += matrix[dy+1][dx+1]*redVal;
- greenSum += matrixl[dy+1][dx+1]*greenVal;
- blueSum += matrix[dy+1][dx+1]*blueVal;
- return color(redSum,greenSum,blueSum);
- }
- }
- }
but i keep getting the error 'the field Component.x is not visible', with line 39 highlighted. I thought that perhaps the image ('target') attributes weren't properly getting passed on to the blur function. However, I tried another simple example:
- //-----------------Globals
- PImage target;
- int rad = 10;
- //-----------------Setup
- void setup() {
- size(1024,683);
- target = loadImage("data/leaf.jpg");
- target.loadPixels();
- int randomX = int(random(target.width));
- int randomY = int(random(target.height));
- int loc = x + y*target.width;
- float r = red(target.pixels[loc]);
- float g = green(target.pixels[loc]);
- float b = blue(target.pixels[loc]);
- noStroke();
- fill(r,g,b,100);
- ellipse(randomX,randomY,rad,rad);
- //save("leaf_blur.png");
Any ideas?
Thank you SO much!
1