Sign in to follow this  
QuadMV

Converting RGB to grey scale

Recommended Posts

QuadMV    168
Is there a function/macro that will take either RGB components, or a D3DCOLOR, and convert it to a greyscale? I'm probably expecting too much, but there seems to be everything else out there. If not, any ideas on what the right technique to do so would be? I have a color texture, but I want to render the texture as a greyscale Thanks much

Share this post


Link to post
Share on other sites
xyzzy00    307
You probably want to just do it in your pixel shader:

float4 main(float2 tc : TEXCOORD0) : COLOR
{
float3 color = tex2D(s, tc);
float grey = dot(color, float3(0.212671f, 0.715160f, 0.072169f));
return float4(grey, grey, grey, 1.0f);
}

xyzzy

Share this post


Link to post
Share on other sites
averisk    146
Well if white is (255, 255, 255) and black is (0, 0, 0) maybe it's as simple as just adding up the RGB values and dividing by 255 * 3

Share this post


Link to post
Share on other sites
ernow    732
Greyscales are just 'colors' with r = g = b.
So there are several ways of calculating the value you are looking for.

1. Quick 'n' dirty: average r, g and b:
gs = (r + g + b) / 3
newColor = RGB(gs, gs, gs)

2. Use HSL as an inbetween. HSL is, like RGB, a way of repesentating colors but not using red, blue and green.
From the PaintShopPro help:
Quote:
Hue The color reflected from an object, such as red, yellow, or orange. Each hue value is assigned based on its position on the color wheel. On the Jasc Color Picker’s Color wheel, colors are assigned counter-clockwise from the top. Red is at the top (value 0) and as you move around the wheel the colors go through orange, yellow, green, blue, purple, and back to red.

Saturation The purity or vividness of the color. Saturation represents the amount of grey in the color, from 0 (entirely grey) to 255 (fully saturated color).

Lightness The perceived amount or intensity of light in the color. Lightness ranges from 0 (no light, or black) to 255 (total lightness, or white). At 50 percent lightness, or a value of 128, a color is considered pure. For example, pure red has a hue of 255, a saturation of 255 (100 percent) and a lightness of 128 (50 percent). For pure blue, the hue is 170, saturation is 255 and lightness is 128.



As you can see lightness is the one you want for your grayscale.

To calulate Lightness:

In .NET
In C

Cheers

Share this post


Link to post
Share on other sites
xyzzy00    307
If you are trying to model how humans percieve brightness, '(R+G+B)/3' is really not the best solution. From colorspace faq:


C-9 WHAT WEIGHTING OF RED, GREEN AND BLUE CORRESPONDS TO BRIGHTNESS?

Direct acquisition of luminance requires use of a very specific spectral
weighting. However, luminance can also be computed as a weighted sum of
red, green and blue components.

If three sources appear red, green and blue, and have the same radiance in
the visible spectrum, then the green will appear the brightest of the three
because the luminous efficiency function peaks in the green region of the
spectrum. The red will appear less bright, and the blue will be the darkest
of the three. As a consequence of the luminous efficiency function, all
saturated blue colors are quite dark and all saturated yellows are quite
light. If luminance is computed from red, green and blue, the coefficients
will be a function of the particular red, green and blue spectral weighting
functions employed, but the green coefficient will be quite large, the red
will have an intermediate value, and the blue coefficient will be the
smallest of the three.

Contemporary CRT phosphors are standardized in Rec. 709 [8], to be
described in section 17. The weights to compute true CIE luminance from
linear red, green and blue (indicated without prime symbols), for the Rec.
709, are these:

Y = 0.212671 * R + 0.715160 * G + 0.072169 * B;

This computation assumes that the luminance spectral weighting can be
formed as a linear combination of the scanner curves, and assumes that the
component signals represent linear-light. Either or both of these
conditions can be relaxed to some extent depending on the application.

Some computer systems have computed brightness using (R+G+B)/3. This is at
odds with the properties of human vision, as will be discussed under What
are HSB and HLS? in section 36.

The coefficients 0.299, 0.587 and 0.114 properly computed luminance for
monitors having phosphors that were contemporary at the introduction of
NTSC television in 1953. They are still appropriate for computing video
luma to be discussed below in section 11. However, these coefficients do
not accurately compute luminance for contemporary monitors.

Share this post


Link to post
Share on other sites
QuadMV    168
Thanks for all the suggestions.

The averaging seems to have worked, expect for one small tidbit, the color isn’t necessarily shades of grey. It’s correctly showing me shades, but in my case the color seems to be lavender. Any tricks to making it grey?


Share this post


Link to post
Share on other sites
QuadMV    168
XYZZY,

Can you explain what this is doing for me?

and, what is the "s" in the tex2D call?

-------------------------

You probably want to just do it in your pixel shader:

float4 main(float2 tc : TEXCOORD0) : COLOR
{
float3 color = tex2D(s, tc);
float grey = dot(color, float3(0.212671f, 0.715160f, 0.072169f));
return float4(grey, grey, grey, 1.0f);
}


Share this post


Link to post
Share on other sites
ReaVeR-Andrey    199
I like these constants more:

inline DWORD toGrayscale ( DWORD color )
{
float r = (float) ( ( color >> 16 ) & 0xff );
float g = (float) ( ( color >> 8 ) & 0xff );
float b = (float) ( color & 0xff );
r *= 0.299f;
g *= 0.587f;
b *= 0.114f;
int sum = (int) ( ( r + g + b ) * 0.8f ) & 0xff;
return ( 0xff000000 | sum | ( sum << 8 ) | ( sum << 16 ) );
}


>prev. post: it is a pixel shader in HLSL. s is a sampler. it is doing nothing to you

Share this post


Link to post
Share on other sites
QuadMV    168
I'm good now. Thanks everyone.

I didn't realize I had the code right all along, but I was adjusting the color by a delta, and I needed to back the delta out before converting it to a grey scale, so now things are working great.

Thanks all, I'll be posting screen shots soon.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this