14 Replies Latest reply on Feb 21, 2007 1:13 PM by 843805

    Color to Grayscale to Binary

    843805
      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 !
        • 1. Re: Color to Grayscale to Binary
          800774
          You can create a [url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/BufferedImage.html]BufferedImage with TYPE_BYTE_BINARY or TYPE_BYTE_GRAY, and draw the color image on this BufferedImage .
          • 2. Re: Color to Grayscale to Binary
            843805
            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.
            • 3. Re: Color to Grayscale to Binary
              843805
              You can create a [url
              http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image
              /BufferedImage.html]BufferedImage 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 !
              • 4. Re: Color to Grayscale to Binary
                843805
                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 ?
                • 5. Re: Color to Grayscale to Binary
                  800774
                  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);
                  • 6. Re: Color to Grayscale to Binary
                    843805
                    Great !
                    This is exactly what I need, but...
                    Why the background becomes black ? (It was pure white in the color image.)
                    • 7. Re: Color to Grayscale to Binary
                      843805
                      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 !
                      • 8. Re: Color to Grayscale to Binary
                        843805
                        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.
                        • 9. Re: Color to Grayscale to Binary
                          843805
                          Thanks !
                          In hexadecimal it is much more understoodable.

                          But, why the background becomes black ??
                          • 10. Re: Color to Grayscale to Binary
                            843805
                            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.
                            • 11. Re: Color to Grayscale to Binary
                              843805
                              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;
                              }
                              • 12. Re: Color to Grayscale to Binary
                                843805
                                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 });
                                • 13. Re: Color to Grayscale to Binary
                                  843805
                                  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.
                                  • 14. Re: Color to Grayscale to Binary
                                    843805
                                    Thank you very much !