Forum Stats

  • 3,825,879 Users
  • 2,260,570 Discussions
  • 7,896,718 Comments

Discussions

Color to Grayscale to Binary

843805
843805 Member Posts: 49,999
edited Feb 21, 2007 8:13AM in Swing
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 !
b8837d97-1e35-4ac0-943e-d9de19fc5fde
«1

Comments

  • 800774
    800774 Member Posts: 1,602
    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 .
  • 843805
    843805 Member Posts: 49,999
    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.
  • 843805
    843805 Member Posts: 49,999
    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 !
  • 843805
    843805 Member Posts: 49,999
    edited Feb 20, 2007 8:45AM
    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 ?
  • 800774
    800774 Member Posts: 1,602
    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);
  • 843805
    843805 Member Posts: 49,999
    Great !
    This is exactly what I need, but...
    Why the background becomes black ? (It was pure white in the color image.)
  • 843805
    843805 Member Posts: 49,999
    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 !
  • 843805
    843805 Member Posts: 49,999
    edited Feb 20, 2007 11:32AM
    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.
  • 843805
    843805 Member Posts: 49,999
    Thanks !
    In hexadecimal it is much more understoodable.

    But, why the background becomes black ??
  • 843805
    843805 Member Posts: 49,999
    edited Feb 21, 2007 6:24AM
    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.
This discussion has been closed.