Read the pixel value of an image as gray level.

Hi all,

I have an image which is converted to gray scale, now i want to read the pixel of the image( which is of the range of 0 to 255) but I am not getting it correct since I am using getrgb() to read the pixel which returns me rgb value as a default.

How can I get the pixel in the range of gray scale as from 0 to 255.

Here is the code that I have written.

{

Image image = ImageIO.read(new File("C:/test1.jpg"));

BufferedImage image1= convert(image);

int w = image1.getWidth();

int h = image1.getHeight();

System.out.println("Height " +h+ ", Width " +w);

int[] rgbs = new int[w*h];

int x = 0;

image1.getRGB(0, 0, w, h, rgbs, 0, w);

for (int i=0; i<2; i++)

for (int j=0;j<2;j++)

{

System.out.println("Pixel " + i +"," + j + "has " + "RGB values of "+ rgbs[x]);

x++;

}

}

//

public static BufferedImage convert(Image im)

{

BufferedImage bi = new BufferedImage(im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_BYTE_GRAY);

Graphics bg = bi.getGraphics();

bg.drawImage(im, 0, 0, null);

bg.dispose();

return bi;

}

Thank you.

[1220 byte] By [ephemerala] at [2007-10-3 2:14:35]
# 1

A couple of ways:

1. get the DataBuffer (BufferedImage.getRaster().getDataBuffer())

This should return an instance of DataBufferByte for your grayscale image.

From this DataBufferByte you can get access to the array of pixels.

DataBufferByte dbb = (DataBufferByte)bm.getRaster().getDataBuffer();

byte pixels[] = dbb.getData();

for (byte pix : pixels) {

System.out.println(pix+" ");

}

2. You can use Raster's getDataElements() methods. For example, to get a 0,0 pixel:

byte pix[] = new byte[1];

bImage.getRaster().getDataElements(0, 0, pix);

System.out.println("pix(0,0):"+pix[0]);

Thanks,

Dmitri

dmitri_trembovetskia at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...
# 2

> A couple of ways:

> 1. get the DataBuffer

> 2. You can use Raster's getDataElements() methods.

3. The R G and B values of a grayscale image are the same for any given pixel. Using getRGB to get the combined RGB data, you can simply mask the int to get the bits corresponding to red, green or blue.

Niceguy1a at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...
# 3
Good point.
dmitri_trembovetskia at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...
# 4
Thank you very much for the solution.
ephemerala at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...
# 5
Thank you very much.
ephemerala at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...
# 6
I added the code into my code,the first second one using Raster's getDataElements but the result I get is not ranging from 0 to 255 but for white value I get -1,for gray its -65 andfor black its 0 which is correct I guess.Thank you.
ephemerala at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...
# 7
Check this thread http://forum.java.sun.com/thread.jspa?threadID=757040&tstart=30
neigora at 2007-7-14 19:13:25 > top of Java-index,Security,Cryptography...