Color to Grayscale to Binary

Hi !Is there a simple way in swing to convert a color Image to grayscale Image, and/or convert a grayscale Image to binary Image ?Thanks !
[159 byte] By [moroshkoa] at [2007-11-26 19:00:20]
# 1
You can create a [url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/BufferedImage.html]BufferedImage[/url] with TYPE_BYTE_BINARY or TYPE_BYTE_GRAY, and draw the color image on this BufferedImage .
Rodney_McKaya at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

If you draw into another image you'll need a big pile of extra heap space though, which can easily become a major issue if you're dealing with large images.

It's not something I've used, but isn't ColorConvertOp what you're after? I'm sure a quick google will reveal how to use it.

itchyscratchya at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

> You can create a [url

> http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image

> /BufferedImage.html]BufferedImage[/url] with

> TYPE_BYTE_BINARY or TYPE_BYTE_GRAY, and draw the

> color image on this BufferedImage.

Thanks for the idea, but I don't understand how to create a binary BufferedImage from a color BufferedImage (or from file, which is better for me). I didn't found such BufferedImage constructor.

I tried to getData() from the color BufferedImage and set this data (using setData()) into (the previously created with TYPE_BYTE_BINARY) binary BufferedImage. Thus results in a very strange picture...

Please Help !

moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 4

> If you draw into another image you'll need a big pile

> of extra heap space though, which can easily become a

> major issue if you're dealing with large images.

What do you mean by large images ? Is 1000*1000 large ?

> It's not something I've used, but isn't

> ColorConvertOp what you're after? I'm sure a quick

> google will reveal how to use it.

ColorConvertOp really helps to create the a grayscale image, but I didn't found how to create a binary image using this method (I did't found the proper constant which can take place of ColorSpace.CS_GRAY).

Here is my code:

public static BufferedImage getGrayScaleImage(String filename) {

ColorConvertOp grayOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);

BufferedImage originalImage = null, grayScaleImage;

File file = new File(filename);

try {

originalImage = ImageIO.read(file);

} catch (IOException e) {

System.out.println("Could not open file: " + filename);

System.exit(1);

}

grayScaleImage = grayOp.filter(originalImage, null);

return grayScaleImage;

}

Can anyone help, please ?

moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 5

BufferedImage image = ImageIO.read(new File("d:/IMG_5167.JPG"));

BufferedImage bwImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);

bwImage.createGraphics().drawImage(image, 0, 0, null);

Rodney_McKaya at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 6
Great ! This is exactly what I need, but...Why the background becomes black ? (It was pure white in the color image.)
moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 7
And one more question:I checked the values in the binary image using getRGB(x, y) and the only 2 values I saw were -1 and -16777216.The question is where are these values from ? Are they constants that defined in some class ?Thanks !
moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 8
the only 2 values I saw were -1 and -16777216.The question is where are these values from ?Er... they're the ARGB values of white and black :o)0xffffffff and 0xff000000 respectively.
itchyscratchya at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 9
Thanks !In hexadecimal it is much more understoodable.But, why the background becomes black ?
moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 10

Were areas of your original image transparent? Remember that by default all int arrays are popuated with zeroes. In the case of an "RGB" format image, that represents opaque black (transparent black in "ARGB"). So when you create a new RGB image, it's opaque black by default.

Otherwise, dunno, if your background was nearly white but not quite (eg 0xfffefefe) maybe the threshold makes any non-white value black. I've not converted to binary images before, only from.

itchyscratchya at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 11

> Were areas of your original image transparent?

> Remember that by default all int arrays are popuated

> with zeroes. In the case of an "RGB" format image,

> that represents opaque black (transparent black in

> "ARGB"). So when you create a new RGB image, it's

> opaque black by default.

>

> Otherwise, dunno, if your background was nearly white

> but not quite (eg 0xfffefefe) maybe the threshold

> makes any non-white value black. I've not converted

> to binary images before, only from.

Thanks !

I think that this is exactly the problem.

The problem is that I can't figure out where exactly I need to set the isOpaque property to true. I thougt maybe to scan the original image with a mask that sets alpha to opaque (is it 0x00 or 0xff ?), but it seems very inefficient to me. Is there another way ? Here is my code:

public static BufferedImage getBinaryImage(String filename) {

BufferedImage originalImage = null, binaryImage;

try {

originalImage = ImageIO.read(new File(filename));

} catch (IOException e) {

System.out.println("Could not open file: " + filename);

System.exit(1);

}

binaryImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);

binaryImage.createGraphics().drawImage(originalImage, 0, 0, null);

return binaryImage;

}

moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 12

you can also try this line of code, this will change your image into gray and back ground will not be affected.

PlanerImage image;

image = JAI.create("bandselect", image, new int[]{ 0, 0, 0 });

Prashant_SDNa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 13

Can't you just pre-fill the target image? After you create it, use getGraphics() and then the Graphics methods setColor() and fillRect() passing the size of the image, before rendering the source image into it. Don't forget to dispose() the Graphics object returned by the Image once you're done with it.

itchyscratchya at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...
# 14
Thank you very much !
moroshkoa at 2007-7-9 20:42:32 > top of Java-index,Desktop,Core GUI APIs...