将RGB图像转换为灰度图像减少了java中的内存 [英] convert a RGB image to grayscale Image reducing the memory in java
本文介绍了将RGB图像转换为灰度图像减少了java中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个RGB bufferedImage bImg。
我想将bImg转换为灰色图像。
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);
我试过这个grayIm但是我无法为这个grayIm设置灰度值。
I have tried this grayIm but i can not set the grayscale values for this grayIm.
推荐答案
一种方法是转换色彩空间(性能不佳):
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);
另一种方法是使用BufferedImage(更好的性能):
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();
最后但并非最不重要的是,最佳表现是使用GrayFilter:
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);
来源: http://www.codebeach.com/2008/03/convert-color-image-to-gray-scale-image.html
编辑:根据Mark的评论。
edit: per Mark's comment.
这篇关于将RGB图像转换为灰度图像减少了java中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文