I have an RGB bufferedImage bImg .
I want to convert bImg to gray image.
BufferedImage grayIm=new BufferedImage(bImg.getWidth(null), bImg.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
I have tried this grayIm but i can not set the grayscale values for this grayIm.
Syntax: File input = new File("digital_image_processing. jpg"); BufferedImage image = ImageIO. read(input); To transform the image from RGB to Grayscale format by using method cvtColor() in the Imgproc class.
The thing is that a grey-scale bitmap image is the same size as a color bitmap image because the data that is used to save the grey colors takes just as much space as the color. The only difference is that grey is just 3 times that same value.
Because it is a one layer image from 0-255 whereas the RGB have three different layer image. So that is a reason we prefer grey scale image instead of RGB.
One way could be to convert the color space (poor performance):
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
BufferedImage image = op.filter(bufferedImage, null);
Another would be to use a BufferedImage, as you do (better performance):
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
Last but not least, the best performance is using a GrayFilter:
ImageFilter filter = new GrayFilter(true, 50);
ImageProducer producer = new FilteredImageSource(colorImage.getSource(), filter);
Image mage = Toolkit.getDefaultToolkit().createImage(producer);
source: http://www.codebeach.com/2008/03/convert-color-image-to-gray-scale-image.html
edit: per Mark's comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With