[英]BufferedImage - getting the value of a pixel in grayscale color model picture
I have an BufferedImage
transformed to grayscale using this code. 我使用此代码将
BufferedImage
转换为灰度。 I usually got the pixel values by BufferedImage.getRGB(i,j)
and the gor each value for R, G, and B. But how do I get the value of a pixel in a grayscale image? 我通常通过
BufferedImage.getRGB(i,j)
获得像素值BufferedImage.getRGB(i,j)
而gor分别获得R,G和B的值。但是,如何获得灰度图像中像素的值呢?
EDIT: sorry, forgot abou the conversion. 编辑:对不起,忘了转换。
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;
}
if you have RGB image so you can get the (Red , green , blue , Gray) values like that: 如果您具有RGB图像,则可以这样获得(Red,green,blue,Gray)值:
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);
and the gray is the average for (r , g , b), like this: 灰色是(r,g,b)的平均值,如下所示:
int gray = (r + g + b) / 3;
but if you convert RGB image(24bit) to gray image (8bit) : 但是,如果将RGB图像(24位)转换为灰度图像(8位):
int gray= img.getRGB(x, y)& 0xFF;/////////will be the gray value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.