2013-04-12 69 views
5

我有一个BufferedImage使用此代码转换为灰度。我通常通过BufferedImage.getRGB(i,j)得到像素值,R,G和B的每个值都是gor。但是,如何获取灰度图像中像素的值?BufferedImage - 获取灰度彩色模型图像中像素的值

编辑:抱歉,忘记了转换。

static BufferedImage toGray(BufferedImage origPic) { 
    BufferedImage pic = new BufferedImage(origPic.getWidth(), origPic.getHeight(), BufferedImage.TYPE_BYTE_GRAY); 
    Graphics g = pic.getGraphics(); 
    g.drawImage(origPic, 0, 0, null); 
    g.dispose(); 
    return pic; 
} 
+0

你可以发布你用来转换它的代码吗? –

+0

“使用此代码”。代码在哪里? –

回答

16

如果你有RGB图像,这样就可以得到(红,绿,蓝,灰)值这样的:

BufferedImage img;//////read the image 
int rgb = img.getRGB(x, y); 
int r = (rgb >> 16) & 0xFF; 
int g = (rgb >> 8) & 0xFF; 
int b = (rgb & 0xFF); 

和灰色​​是平均(R,G,B ),像这样:

int gray = (r + g + b)/3; 

,但如果转换RGB图像(24位),以灰度图像(8位):

int gray= img.getRGB(x, y)& 0xFF;/////////will be the gray value 
+0

有兴趣知道如何获得像素的alpha值 –

+0

@SriHarshaChilakapati alpha是(rgb >> 24)&0xFF – Kajzer

+0

alpha:(rgb >> 24)&0xFF –

相关问题