greyscale an image

Hi,

I have loaded an image, but before doing anyother processing I want to greyscale it. I have read the api but can't find a clear example:

File file =new File(fLoci);

loadImage = ImageIO.read(file);

greyscale loadImage?

I know you can use BufferedImage.TYPE_BYTE_GRAY for a bufferedimage, I am just not sure how to apply it in this situation.

Many thanks, Ron

Message was edited by:

cake

[480 byte] By [cakea] at [2007-11-27 2:13:42]
# 1

Here's a small, simple method I found online.

public static BufferedImage convertToGrayscale(BufferedImage source) {

BufferedImageOp op = new ColorConvertOp(

ColorSpace.getInstance(ColorSpace.CS_GRAY), null);

return op.filter(source, null);

}

Here's the link.

http://www.jguru.com/faq/view.jsp?EID=221919

CaptainMorgan08a at 2007-7-12 2:09:19 > top of Java-index,Java Essentials,Java Programming...
# 2

Here's two ways. I don't know why they produce different results.

import java.awt.*;

import java.awt.color.*;

import java.awt.image.*;

import java.io.*;

import java.net.*;

import javax.imageio.*;

import javax.swing.*;

public class BWExample implements Runnable {

public void run() {

BufferedImage im1 = getImage();

JPanel p = new JPanel();

p.add(new JLabel(new ImageIcon(im1)));

p.add(new JLabel(new ImageIcon(toBW1(im1))));

p.add(new JLabel(new ImageIcon(toBW2(im1))));

JFrame f = new JFrame("BWExample");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setContentPane(p);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

BufferedImage getImage() {

try {

URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");

return ImageIO.read(url);

} catch (IOException e) {

throw new RuntimeException(e);

}

}

BufferedImage toBW1(BufferedImage im) {

int w = im.getWidth();

int h = im.getHeight();

BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D g = result.createGraphics();

g.drawRenderedImage(im, null);

g.dispose();

return result;

}

BufferedImage toBW2(BufferedImage im) {

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);

return new ColorConvertOp(cs, null).filter(im, null);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new BWExample());

}

}

DrLaszloJamfa at 2007-7-12 2:09:19 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi,

I tried thi:

int w = im.getWidth();

int h = im.getHeight();

BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

and i ended up with a black image?!

i'll try the other, i wonder if it was the downsample that was doing it!

Thanks, Ron

cakea at 2007-7-12 2:09:20 > top of Java-index,Java Essentials,Java Programming...
# 4
Did you use *all* the code in toBW1?
DrLaszloJamfa at 2007-7-12 2:09:20 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi, yes I did, and I still ended up with a black image.

entryImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D g = entryImage.createGraphics();

g.drawRenderedImage(entryImage, null);

g.dispose();

The convertToGrayscale function works though.

I will try with img2 now.

One other question, what is the accepted cutoff point for 'white' background. So if a pixel is ? int value it is considered white, and > than that and you would say it is black, this is obviously thinking in monochrome now?

Thanks, Ron

Message was edited by:

cake

cakea at 2007-7-12 2:09:20 > top of Java-index,Java Essentials,Java Programming...
# 6

Your code:

entryImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D g = entryImage.createGraphics();

g.drawRenderedImage(entryImage, null);

g.dispose();

My code:

BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D g = result.createGraphics();

g.drawRenderedImage(im, null);

g.dispose();

Do you see your mistake? I am rendering the original im onto result.

You are rendering the new entryImage onto entryImage -- black on black!

> One other question, what is the accepted cutoff point for 'white' background.

That sounds like a judgment call to me.

DrLaszloJamfa at 2007-7-12 2:09:20 > top of Java-index,Java Essentials,Java Programming...