将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
根据马克的评论.
这篇关于将RGB图像转换为灰度图像以减少java中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文