#BufferedImage getRGB help

1 messages · Page 1 of 1 (latest)

ivory ravine
#

This image always output zero for rgb peepo_sad

File input = new File(pathToImage);
Assertions.assertTrue(input.canRead());
BufferedImage image = ImageIO.read(input);
int width = image.getWidth();
int height = image.getHeight();
System.out.println(width);
System.out.println(height);
nbIndividu = Math.min(width * height, nbIndividu);
double [] [] decodedImage = new double [nbIndividu] [3] ;
int i = 0;
for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
    int rgb = image.getRGB(x, y);
dry vineBOT
# ivory ravine This image always output zero for rgb <:peepo_sad:871689368983707648> ```java ...

Detected code, here are some useful tools:

Formatted code
File input = new File(pathToImage);
Assertions.assertTrue(input.canRead());
BufferedImage image = ImageIO.read(input);
int width = image.getWidth();
int height = image.getHeight();
System.out.println(width);
System.out.println(height);
nbIndividu = Math.min(width * height, nbIndividu);
double [] [] decodedImage = new double [nbIndividu] [3] ;
int i = 0;
for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
    int rgb = image.getRGB(x, y);
#

<@&987246399047479336> please have a look, thanks.

dry vineBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

reef copper
#

Could you upload the image here? @ivory ravine

ivory ravine
#

I'm trying to affect each pixel value to a 2D array where red, green, blue are columns.

#

But the RGB value encoded between 0 and 255 always returns 0 for PNG and -1 for JPEG.

#

Thanks @reef copper

regal fulcrum
#

Thats because your png image is transparent to 80%

#

So of cours there is just no color on that spots

rapid acorn
#

i tink you need to compile x,y

#

NVM

#

its double rounds

rapid acorn
#

so you get:

for (int x = 0; x<width; x++);{
for(int y=0; y<width; y++);{
//in these upcoming operators they wll nested where all variables are defined
int rgb = getImage(x,y);
decodedImage[i][0] = (rgb>>16) & 0xFF;
decodedImagine[i][1] = (rgb>>8) &0xFF;
decodedImage[i][2]= (rgb >>0) & 0xFF;
i++;
}
}

dry vineBOT
# rapid acorn so you get: for (int x = 0; x<width; x++);{ for(int y=0; y<width; y++);{ ...

Detected code, here are some useful tools:

Formatted code
so you get : for (int x = 0; x < width; x++);
  {
  for (int y = 0; y < width; y++);
    {
    //in these upcoming operators they wll nested where all variables are defined
    int rgb = getImage(x, y);
    decodedImage[i] [0]  = (rgb >> 16) & 0xFF;
    decodedImagine[i] [1]  = (rgb >> 8) & 0xFF;
    decodedImage[i] [2]  = (rgb >> 0) & 0xFF;
    i++;
  }
}
ivory ravine
#

I think I need to change the color model of the picture.

#

I also converted the picture into JPEG

#

And it won't read it.

#

Perhaps I should try another color profile

ivory ravine
#

I found this nice workaround using Raster from BufferedImage::getData:

public static Population process(String pathToImage, int nbIndividu) throws IOException
        {
        BufferedImage image;
            {
            File file = new File(pathToImage);
            InputStream input = new FileInputStream(file.getAbsoluteFile());
            ImageInputStream stream = ImageIO.createImageInputStream(input);
            image = ImageIO.read(stream);
            }

        int height = image.getHeight();
        int width = image.getWidth();

        nbIndividu = Math.min(width * height, nbIndividu);

        double[][] decodedImage = new double[nbIndividu][4];

        Raster data = image.getData();

        int i = 0;
        for(int x = 0; x < width; x++)
            {
            for(int y = 0; y < height; y++, i++)
                {
                if (i >= nbIndividu)
                    {
                    return new Population(decodedImage);//
                    }

                data.getPixel(x, y, decodedImage[i]);
                }
            }

        return null;
        }
dry vineBOT
# ivory ravine I found this nice workaround using Raster from `BufferedImage::getData`: ```java...

Detected code, here are some useful tools:

Formatted code
public static Population process(String pathToImage, int nbIndividu) throws IOException {
  BufferedImage image;
    {
    File file = new File(pathToImage);
    InputStream input = new FileInputStream(file.getAbsoluteFile());
    ImageInputStream stream = ImageIO.createImageInputStream(input);
    image = ImageIO.read(stream);
  }
  int height = image.getHeight();
  int width = image.getWidth();
  nbIndividu = Math.min(width * height, nbIndividu);
  double [] [] decodedImage = new double [nbIndividu] [4] ;
  Raster data = image.getData();
  int i = 0;
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++, i++) {
      if (i >= nbIndividu) {
        return new Population(decodedImage);
        //
      }
      data.getPixel(x, y, decodedImage[i] );
    }
  }
  return null ;
}
ivory ravine
#

It works now

#

[4] -> is for RGBA; red, green, blue, alpha