Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
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 !
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 !
Comments
-
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 .
-
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. -
You can create a [urlTYPE_BYTE_BINARY or TYPE_BYTE_GRAY, and draw the
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image
/BufferedImage.html]BufferedImage with
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 ! -
If you draw into another image you'll need a big pileWhat do you mean by large images ? Is 1000*1000 large ?
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'tColorConvertOp 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).
ColorConvertOp what you're after? I'm sure a quick
google will reveal how to use it.
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 ? -
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);
-
Great !
This is exactly what I need, but...
Why the background becomes black ? (It was pure white in the color image.) -
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 ! -
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)
0xffffffff and 0xff000000 respectively. -
Thanks !
In hexadecimal it is much more understoodable.
But, why the background becomes black ?? -
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.