首页 > 代码库 > Java 彩色图转灰度图

Java 彩色图转灰度图

1. 方法1
BufferedImage grayImage = new BufferedImage(width, height, colorImage.TYPE_BYTE_GRAY);  
Graphics g = grayImage.getGraphics();  
g.drawImage(colorImage, 0, 0, null);  
g.dispose();

 

2. 方法2(不推荐)

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);  
ColorConvertOp op = new ColorConvertOp(cs, null);  
BufferedImage grayImage = op.filter(colorImage, null);

注意这种方法得到的灰度图grayImage与彩色图colorImage的通道数一样。


出处: http://stackoverflow.com/questions/9131678/convert-a-rgb-image-to-grayscale-image-reducing-the-memory-in-java



Java 彩色图转灰度图