简体   繁体   中英

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?

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. 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.

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. There are much faster techniques available.

UPDATE

在此处输入图片说明

Here's an example image with different scaling factors for R, G, and B applied. 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. 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. A common way is to do (R+G+B)/3 Others are computing some luminance Luminance measure (Lab, YUV, HSV)

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

var intValue = cl.R;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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