3
BufferedImage image = ImageIO.read(new File(img path));  

int width = image.getWidth();
int height = image.getHeight();
int[][] result = new int[height][width];

for (int row = 0; row < height; row++) {
  for (int col = 0; col < width; col++) {
     result[row][col] = image.getRGB(row, col);
  }
}

and this is the exception I get :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:301)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
at PlantExtraction.main(PlantExtraction.java:46)

How can I remove these exceptions ?

2
  • 2
    run it in a debugger, so you can see which index is out of bounds when the exception is thrown. My guess is that row and column are reversed -- wouldn't the row be "Y" and the column be "X"? Commented Dec 3, 2014 at 5:39
  • IMHO the issue is at image.getRGB(row,col). See if row is related to height and col is related to width. To confirm you can limit row with width and col with height. Commented Dec 3, 2014 at 5:59

1 Answer 1

6

The code

image.getRGB(row, col); 

Should be

image.getRGB(col, row);

As the documentation says:

getRGB(int x, int y).

Documentation

(your col value is running upto width - which is the x-maximum of the image, so use col for x and row for y)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.