简体   繁体   English

如何获取灰度图像中的像素值

[英]How to get the pixel value in a grayscale image

I have a BufferedImage which is of TYPE_BYTE_GRAY and I need to get the pixel value at x,y. 我有一个TYPE_BYTE_GRAY的BufferedImage,我需要得到x,y的像素值。 I know I can't use getRGB as it returns the wrong color model so how do I go about it? 我知道我不能使用getRGB,因为它返回错误的颜色模型,所以我该怎么办呢? Many thanks! 非常感谢!

Get java.awt.image.Raster from BufferedImage by invoking getData() method. 通过调用getData()方法从BufferedImage获取java.awt.image.Raster

Then use 然后用

int getSample(int x, int y, int b)

on received object, where b is the color channel (where each color is represented by 8 bits). 在接收的对象上,其中b是颜色通道(其中每种颜色由8位表示)。

For gray scale 适用于灰度

b = 0.

For RGB image 对于RGB图像

b = 0 ==>> R channel,
b = 1 ==>> G channel,
b = 2 ==>> B channel.

I guess what you looking for is the math to get a one number to represent the Gray scale in that RGB, there are few diff ways, follow some of them: 我想你要找的是用一个数字来表示RGB中的灰度等级的数学,几乎没有差异,跟随其中一些:

The lightness method averages the most prominent and least prominent colors: (max(R, G, B) + min(R, G, B)) / 2. 亮度方法平均最突出和最不显着的颜色:(max(R,G,B)+ min(R,G,B))/ 2。

The average method simply averages the values: (R + G + B) / 3. 平均方法简单地平均值:(R + G + B)/ 3。

The luminosity method is a more sophisticated version of the average method. 发光度方法是普通方法的更复杂版本。 It also averages the values, but it forms a weighted average to account for human perception. 它还对这些值进行平均,但它形成了一个加权平均值来说明人类的感知。 We're more sensitive to green than other colors, so green is weighted most heavily. 我们对绿色比其他颜色更敏感,所以绿色最重要。 The formula for luminosity is 0.21 R + 0.71 G + 0.07 B. 发光度的公式为0.21 R + 0.71 G + 0.07 B.

Reference : http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/ 参考: http//www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/

Provided that you have a BufferedImage named grayImg whose type is TYPE_BYTE_GRAY 前提是您有一个名为grayImg的BufferedImage,其类型为TYPE_BYTE_GRAY

int width = grayImg.getWidth();

int height = grayImg.getHeight();

byte[] dstBuff = ((DataBufferByte) grayImg.getRaster().getDataBuffer()).getData();

Then the gray value at (x,y) would simply be: 那么(x,y)处的灰度值就是:

dstBuff[x+y*width] & 0xFF;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM