Results 1 to 3 of 3
Thread: Graysacling an image?
- 11-07-2011, 10:11 PM #1
Graysacling an image?
I have imported an image(into a BufferedImage), I want to grayscale this image. How can I do it?
One guy said something like this:
But I have no clue how to do this. BufferedImage store its RBG in a int. If I could use WriteableRaster and use the formula above, but I dont know how to convert a WriteableRaster to BufferedImage.The way to convert an RGB value to grayscale is to use the following formula: brightness = 0.212671 * R + 0.715160 * G + 0.072169 * B. Assign brightness to the Red, Green, and Blue components of the a new RGB value.
Some help?
- 11-07-2011, 10:31 PM #2
Re: Graysacling an image?
This work:
But the quality is poorJava Code:public static void main(String[] args) throws Exception { BufferedImage img = ImageIO.read(new File ("C:\\ngchem.png")); BufferedImage grayimg = new BufferedImage (img.getWidth (), img.getHeight (), BufferedImage.TYPE_BYTE_GRAY); for (int i = 0; i < img.getHeight (); i++) for (int j = 0; j < img.getWidth (); j++) { int pixel = img.getRGB (j, i); grayimg.setRGB (j, i, pixel); } ImageIO.write(grayimg, "PNG", new File ("C:\\test.png")); }
- 11-07-2011, 11:31 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Graysacling an image?
See the following class: ColorConvertOp (Java Platform SE 6)
It makes convertion of Images to different color spaces much easier, you just need to specify which ColorSpace's for the conversion, and supply the appropriate BufferedImages of a given type...the example below demonstrates an example of how to accomplish this.
I'd recommend immersing yourself in the API of these classes and methods, as there are many different options you can set for the conversion.Java Code:BufferedImage image = //get color image BufferedImage bw = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); BufferedImageOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), ColorSpace.getInstance(ColorSpace.CS_GRAY), null); bw = op.filter(image, bw);
Similar Threads
-
How to import image, draw circles/text on it, and save a new image to disk
By InTheEndo in forum Java 2DReplies: 1Last Post: 07-28-2011, 08:48 AM -
Java Applet loading image; render as you get image?
By ea25 in forum New To JavaReplies: 12Last Post: 04-14-2011, 01:58 PM -
Rotating Buffered Image distorts image
By VortexSpin in forum Java 2DReplies: 1Last Post: 02-13-2011, 05:54 AM -
[SOLVED] manipulating the pixel values of an image and constructinf a new image from
By sruthi_2009 in forum AWT / SwingReplies: 14Last Post: 04-10-2009, 08:46 AM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks