I have an BufferedImage
transformed to grayscale using this code. I usually got the pixel values by BufferedImage.getRGB(i,j)
and the gor each value for R, G, and B. But how do I get the value of a pixel in a grayscale image?
EDIT: sorry, forgot abou the conversion.
static BufferedImage toGray(BufferedImage origPic) {
BufferedImage pic = new BufferedImage(origPic.getWidth(), origPic.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = pic.getGraphics();
g.drawImage(origPic, 0, 0, null);
g.dispose();
return pic;
}
if you have RGB image so you can get the (Red , green , blue , Gray) values like that:
BufferedImage img;//////read the image
int rgb = img.getRGB(x, y);
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb & 0xFF);
and the gray is the average for (r , g , b), like this:
int gray = (r + g + b) / 3;
but if you convert RGB image(24bit) to gray image (8bit) :
int gray= img.getRGB(x, y)& 0xFF;/////////will be the gray value