Finding the edges in an Image error

I’ve written this code to find the edges in my image which I have named properly in the correct file:

<PImage frog;

void setup(){
size(1200,720);
frog=loadImage(“frog.jpg”);
}

void draw(){
loadPixels();
frog.loadPixels();
for(int x=0; x<width-4; x++){
for(int y=0; y<height; y++){
int loc1=x+y*width;
int loc2=(x+1)*width;
float b1=brightness(frog.pixels[loc1]);
float b2=brightness(frog.pixels[loc2]);
float diff=abs(b1-b2);
pixels[loc1]=color(diff);
}
}
updatePixels();
}

And I keep getting this error:
‘‘ArrayIndexOutOfBoundsException: 864000
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting.’’

You’re locations at some points are not found within the pixel array. Add some bools to check if locations are greater than 0 and less than pixel array length.

1 Like

What do you mean books? sorry i’m new here

Soz autocorrect. Meant bools.

you mean in the form of if diff<20 for example?

Yes. You just need to check you’re locations are within the pixel array

I don’t understand exactly how I can write this. If the sketch doesn’t run in the first place how is it supposed to run after I write it?

You need to place

float b1=brightness(frog.pixels[loc1]);
float b2=brightness(frog.pixels[loc2]);
float diff=abs(b1-b2);
pixels[loc1]=color(diff);

Within appropriate statements

how would that look like?

if(condition1) {

} 

if(condition2) {

} 

Or

if(condition1 && condition2) {

} 

This should be fairly simple as I’ve already detailed what the conditions are.

Turns out I had forgotten to write (x+1)+y*width. It was the +y I had forgotten about damn my eyes are worthless

1 Like

If we could only get back the hours spent debugging eh.