Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedImage - getting the value of a pixel in grayscale color model picture

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;
}
like image 414
Kajzer Avatar asked Apr 12 '13 13:04

Kajzer


1 Answers

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
like image 137
Alya'a Gamal Avatar answered Oct 19 '22 00:10

Alya'a Gamal