Strange RGB value from java BufferedImage getRGB ()

I am trying to get the RGB value from a grayscale image, and that was the wrong (?) RGB value. Here is the code.

Color color = new Color(image.getRGB(0, 0));
System.out.print(color.getRed());
System.out.print(color.getGreen());
System.out.print(color.getBlue());

The color picker used the first RGB pixel value R:153,G:153,B:153, but my code printed

203203203

Why did this happen? And also, I'm trying to use the MATLAB grayscale values ​​for the exact pixel, is also 153. Am I doing this wrong?

this image

enter image description here

+4
source share
2 answers

This is because, image.getRGB(x, y)by definition, it returns ARGB values ​​in the sRGB color space.

From JavaDoc :

RGB (TYPE_INT_ARGB) sRGB . , ColorModel.

Matlab , , RGB , .

Java, (TYPE_BYTE_GRAY), Raster getDataElements.

Object pixel = raster.getDataElements(0, 0, null); // x, y, data array (initialized if null)

TYPE_BYTE_GRAY, pixel byte .

int grayValue = ((byte[]) pixel)[0] & 0xff;

153 .

+5

System.out.println(image.getRaster().getSample(0, 0, 0));  //R
System.out.println(image.getRaster().getSample(0, 0, 1));  //G
System.out.println(image.getRaster().getSample(0, 0, 2));  //B

getSample(int x, int y, int b)
, (x, y), int. [ this]

:
x - X
y - Y
b -
b = [0,1,2] [R, G, B]

BufferedImage getRGB vs getSample

+2

Source: https://habr.com/ru/post/1609159/


All Articles