How to get pixel value in grayscale image

I have a BufferedImage that has TYPE_BYTE_GRAY, and I need to get the pixel value in x, y. I know I can’t use getRGB because it returns the wrong color model, so how can I do this? Thank you very much!

+4
source share
3 answers

Get java.awt.image.Raster from BufferedImage by calling the getData() method.

Then use

 int getSample(int x, int y, int b) 

on the resulting object, where b is a color channel (where each color is represented by 8 bits).

For grayscale

 b = 0. 

For RGB image

 b = 0 ==>> R channel, b = 1 ==>> G channel, b = 2 ==>> B channel. 
+2
source

I assume that you are looking for math to get a single number to represent the gray scale in this RGB, there are several ways to compare, follow some of them:

The lightness method averages the most noticeable and least known colors: (max (R, G, B) + min (R, G, B)) / 2.

The middle method simply averages the values: (R + G + B) / 3.

The luminosity method is a more complex version of the middle method. It also averages values, but it forms a weighted average to account for human perception. They were more sensitive to green than other colors, so the green weight is most heavily loaded. The formula for luminosity is 0.21 R + 0.71 G + 0.07 B.

Link: http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/

+1
source

If you have a BufferedImage named grayImg whose type is TYPE_BYTE_GRAY

 int width = grayImg.getWidth(); int height = grayImg.getHeight(); byte[] dstBuff = ((DataBufferByte) grayImg.getRaster().getDataBuffer()).getData(); 

Then the gray value in (x, y) would be simple:

 dstBuff[x+y*width] & 0xFF; 
0
source

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


All Articles