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 ?
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)
Related
I have to write a method that:
Create a histogram
read all pixel values from grayscale images (variable width and height)
fill the histogram
How can I do it? I wrote a bit of code but I'm deadlocked.
public histogram(BufferedImage image){
WritableRaster writableRaster = image.getRaster();
int width = image.getWidth();
int height = image.getHeight();
int pixelValue;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelValue = writableRaster.setDataElements(x, y, width, height, );
}
}
}
The following code snippet should help:
Raster raster = image.getRaster();
int numBands = raster.getSampleModel().getNumBands();
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int[] pixelValue = new int[numBands];
raster.getPixel(x, y, pixelValue);
}
}
If you really have a grayscale image without alpha, the SampleModel will contain only one band. In this case the pixelValue array will contain your desired int value. You only have to add an histogram array of 256 int values and increase the value at the index of your pixel value.
I'm trying to get this code to run, but I keep getting an error saying:
at main.Main.flipVertically(Main.java:403) which is the code below.
img[row][col] = img[height - row - 1][col];
I don't know what's wrong with the code or the error they are talking about.
Here's the rest of the code:
public static int[][] flipVertically(int[][] img) {
String dir = "image.jpg";
img = Util.readImageToMatrix(dir);
int height = img[0].length;
int width = img.length;
for(int row = 0; row < height/2; row++)
{
for(int col = 0; col < width; col++)
{
int p = img[row][col];
img[row][col] = img[height - row - 1][col];
img[height - row - 1][col] = p;
}
}
return img;
}
height and width swapped
int height = img.length;
int width = img[0].length;
you souldnt read the matrix in the loop and use the parameter img from function input, or better create a new matrix.
you can swap entire rows like:
public static void flipVertically(int[][] img) {
int height = img.length;
int width = img[0].length;
for(int row = 0; row < height/2; row++)
{
int [] myrow = img[row];
img [row] = img[height - row - 1];
img[height - row - 1] = myrow;
}
}
i wrote this method:
public static Bitmap matrixToBitmap(int[][] slika)
{
int w = slika[0].length;
int h = slika[1].length;
Bitmap into = Bitmap.createBitmap(w, h, Config.ARGB_8888);
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
if(slika[x][y] < 128)
into.setPixel(x, y, Color.BLACK);
else
into.setPixel(x, y, Color.WHITE);
}
}
return into;
}
and when I call it inside my android app with an int[454][454] array, it says this in Logcat:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=452; index=452
pointing at this line of matrixToBitmap method:
if(slika[x][y] < 128)
Can someone figure why is it happening? Values for w and h become 454 and 454, just as they should be.
error is here:
int w = slika[0].length;
int h = slika[1].length;
what happened is, you set up length of first row from your array to be w, and length of your second row to be h
to make it work, change it to:
int w = slika.length;
int h = slika[0].length;
I've been trying to create a sprite for our game but the problem is when i tried to run the program this error appears
Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at really.sprite.TrueSprite.<init>(TrueSprite.java:31)
at really.sprite.Sprite.main(Sprite.java:13)
here's the program that causes the error:
public class TrueSprite
{
BufferedImage spriteSheet = ImageIO.read(new File("resources/robin.png"));
int width = 240, height = 314, rows = 5 , columns = 5;
BufferedImage[] sprites = new BufferedImage[rows * columns];
public TrueSprite(int width, int height, int rows, int columns) throws IOException
{
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
sprites[(i * columns) + j ] = spriteSheet.getSubimage(i * width, j * height, width, height);
}
}
}
public void paint(Graphics g)
{
g.drawImage(sprites[1], 314, 240, null);
}
}
the image size I imported is 1200 x 1570.
I'm having trouble getting my method to work. The method should mirror any image I choose on its diagonal to produce a mirror effect, but at the moment it just produces the same image unedited and I don't what I'm doing wrong. Any help would be greatly appreciated. Thank you.
public Picture mirrorImageDiagonal() {
int size = this.getWidth();
Pixel rightPixel = null;
Pixel leftTargetPixel = null;
Pixel rightTargetPixel = null;
Picture target = new Picture(size, size);
for (double x = 0; x < size; x ++) {
for (double y = 0; y <= x; y ++) {
int yIndex = Math.min((int) y, this.getHeight() - 1);
int xIndex = Math.min((int) x, this.getWidth() - 1);
leftTargetPixel = target.getPixel(yIndex, xIndex);
rightTargetPixel = target.getPixel(xIndex, yIndex);
rightPixel = this.getPixel(xIndex, yIndex);
rightTargetPixel.setColor(rightPixel.getColor());
leftTargetPixel.setColor(rightPixel.getColor());
}
}
return target;
}
I am assuming that you are trying to complete the challenge for A6 in the picture lab packet. I just completed this for school, but if you are not, I hope this still helps you.
public void mirrorDiagonal()
{
Pixel[][] pixels = this.getPixels2D();
Pixel pixel1 = null;
Pixel pixel2 = null;
int width = pixels[0].length;
for (int row = 0; row < pixels.length; row++)
{
for (int col = 0; col < width; col++)
{
if (col < pixels.length)
{
pixel1 = pixels[row][col];
pixel2 = pixels[col][row];
pixel1.setColor(pixel2.getColor());
}
}
}
}