Storing data using an array
in
Programming Questions
•
10 months ago
Hi all,
For my class, I'm analyzing the pixels in an image and storing the coordinates of any pixel that surpasses a particular brightness threshold. I kept getting the "array index out of bounds" error, which had something to do with the number 1031 (which happens to be the dimensions of my image-- 1031x1031). I've since changed the code so my arrays are
< img.length or
img.width, but now it seems that it isn't analyzing the entire image (I make this conclusion because the ellipses I draw around these points only cover 1/4 of the image).
What I want to do:
analyze the image and
store the x,y location of any points that are >= a given brightness threshold
into a set of arrays so that I may recall these points to be used in a series of lines or vertices in the beginShape()/endShape() function.
How do I successfully store data into an array?
Here is my code:
- PImage img;
- int[] x;
- int[] y;
- int n=0;
- void setup(){
- noLoop();
- img = loadImage("backgroundpic.png");
- size(img.width,img.height);
- image(img,0,0,img.width,img.height);
- x = new int[img.width];
- y = new int[img.height];
- }
- void draw(){
- for (int a=0; a <= img.width; a++){
- for (int b=0; b <= img.height; b++){
- color c = img.get(a,b);
- float d = brightness(c);
- fill(255,0,180,13);
- noStroke();
- if (d>100 && n < y.length && n < x.length){
- ellipse (a,b,10,10);
- x[n]=a;
- y[n]=b;
- n++;
- }
- }}
- }
1