简体   繁体   English

我怎么能灰度图像像素的整数值(范围0-255)

[英]How can I can the integer value (range 0-255) of a grayscale image pixel

Hi I want to get the integer values (0-255 range) of a gray scale image ....this code shows me the R,G,B values not one value..how can i get it? 嗨,我想获取灰度图像的整数值(0-255范围)..此代码向我展示了R,G,B值,而不是一个值..我如何获得它?

Bitmap temp1 = image1;
for (int i = 0; i < temp1.Height; i++)
{
    for (int j = 0; j < temp1.Width; j++)
    {
        Color cl = new Color();
        cl = temp1.GetPixel(i, j);
    }
}

If your source image is greyscale and you just want the level of greyness, just pick any of the three components. 如果您的源图像是灰度图像,而您只需要灰度等级,则只需选择三个分量中的任何一个即可。 They will be equal. 他们将是平等的。

If your source image is color but you want to get the grey equivalent, you can convert your color to a grey value in the range 0..255 by blending the red, green and blue color components together. 如果您的源图像是彩色的,但您希望获得等效的灰度,则可以通过将红色,绿色和蓝色的成分混合在一起,将颜色转换为0..255范围内的灰度值。 The blending factors are different because the human eye has different sensitivity to the three primary colors. 混合因素不同,因为人眼对三种原色的敏感性不同。 For fun, try varying the factors (eg use 0.3333 for each) and see what the result looks like. 有趣的是,尝试改变这些因子(例如,每个因子使用0.3333),然后看一下结果。

Color cl = c.GetPixel(i, j); // No need to separately allocate a new Color()
int greyValue = (int)((cl.R * 0.3) + (cl.G * 0.59) + (cl.B * 0.11));
Color grey = Color.FromArgb(cl.A, greyValue, greyValue, greyValue);

Note that it is quite slow to loop through a larger Bitmap, using GetPixel() on each pixel. 请注意,在每个像素上使用GetPixel()遍历较大的Bitmap相当慢。 There are much faster techniques available. 有许多更快的技术可用。

UPDATE 更新

在此处输入图片说明

Here's an example image with different scaling factors for R, G, and B applied. 这是示例图像,其中应用了针对R,G和B的不同缩放比例。 The image will always be greyscaled because the same numeric value is used for each RGB component in the modified image, but the relative lightness does change. 由于修改后的图像中的每个RGB组件都使用相同的数值,因此该图像将始终为灰度级,但是相对亮度会发生变化。 The middle image uses scaling factors suitable for the human eye. 中间图像使用适合人眼的缩放因子。 Note how blue areas in the original image seem oversaturated in the rightmost version. 请注意,在最右边的版本中,原始图像中的蓝色区域显得过饱和。

There are multiple ways to get grayscale from RGB. 有多种方法可以从RGB获取灰度。 A common way is to do (R+G+B)/3 Others are computing some luminance Luminance measure (Lab, YUV, HSV) 一种常见的方法是(R + G + B)/ 3其他方法正在计算某种亮度的亮度量度(Lab,YUV,HSV)

只需读取属性R或G或B,它们中的任何一个都将具有相同的值。

var intValue = cl.R;

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

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