Board index » cppbuilder » RGB to Greyscale.

RGB to Greyscale.

Hi!
I need to convert RGB colors to greyscale. How can I do that?
Also any idea on how to make it also blue, red, green etc. -scale would be
appreciated.
I use BCC++ 5.5 (free commandline tools), so I do not have all thsese VCL,
OWL and other nice stuff so plain API code would be the only helpful. Point
me to the code, please, or post one.
Thanks in advance.
 

Re:RGB to Greyscale.


Quote
> I need to convert RGB colors to greyscale.

Hello Michael, this C function can help you.

If this don't is enough, tellme please; by exaple, maybe you require real
time image processing.

#include <windows.h>
#include <math.h>

BYTE GetIntensity(COLORREF color) {
  BYTE r;
  BYTE g;
  BYTE b;
  BYTE i;
/** each COLORREF (or simply color) is a vector of three components  (red,
green and blue).*/

  r=  getRValue(color); /**retrieve red component*/
  g= getGValue(color); /**and green component */
  b= getBValue(color); /** also blue */

  i= sqrt( r*r +g*g +b*b )/sqrt(3.0);  /**compare this operation with to
obtain vectorial norm.*/
  return (BYTE)i;

Quote
}

An exanple to use is:
  COLORREF color= GetPixel(dc,x,y); /** someone Device Context, someone
position inside */
  BYTE gray= GetIntensity(color);
  SetPixel(dc,gray,gray,gray);
And, how you must think; the color pixel in x,y position is rempalced by
another in gray scale.

Regards.

Tigrux.
ICQ #45507615
sflo...@fcfm.uanl.mx

Re:RGB to Greyscale.


Thanx.
Sandino Flores Moreno <sflo...@fcfm.uanl.mx> wrote in message
news:39a1b90c_2@dnews...
Quote
> > I need to convert RGB colors to greyscale.
> Hello Michael, this C function can help you.

> If this don't is enough, tellme please; by exaple, maybe you require real
> time image processing.

> #include <windows.h>
> #include <math.h>

> BYTE GetIntensity(COLORREF color) {
>   BYTE r;
>   BYTE g;
>   BYTE b;
>   BYTE i;
> /** each COLORREF (or simply color) is a vector of three components  (red,
> green and blue).*/

>   r=  getRValue(color); /**retrieve red component*/
>   g= getGValue(color); /**and green component */
>   b= getBValue(color); /** also blue */

>   i= sqrt( r*r +g*g +b*b )/sqrt(3.0);  /**compare this operation with to
> obtain vectorial norm.*/
>   return (BYTE)i;
> }

> An exanple to use is:
>   COLORREF color= GetPixel(dc,x,y); /** someone Device Context, someone
> position inside */
>   BYTE gray= GetIntensity(color);
>   SetPixel(dc,gray,gray,gray);
> And, how you must think; the color pixel in x,y position is rempalced by
> another in gray scale.

> Regards.

> Tigrux.
> ICQ #45507615
> sflo...@fcfm.uanl.mx

Re:RGB to Greyscale.


On Mon, 21 Aug 2000 18:28:53 -0500, "Sandino Flores Moreno"

Quote
<sflo...@fcfm.uanl.mx> wrote:

[snip]
Quote
>  i= sqrt( r*r +g*g +b*b )/sqrt(3.0);  /**compare this operation with to
>obtain vectorial norm.*/

[snip]

Hi,

    you have to use one of the follwing two formulas in order
to pay attention for CRT phosphors and human eyes when
converting to grayscale:

Y = 0.114 * Blue + 0.587 * Green + 0.299 * Red

or

Y = 0.072169 * Blue  + 0.715160 * Green + 0.21267 * Red

(second preferred)

where Y is the luminance level.

Then you have to rebuild TColor in this manner:

TColor( (int)Y * (65536 + 256 + 1) );

Hope this helps.

Ciao!

Giuliano

Re:RGB to Greyscale.


Quote
Giuliano <gcardin...@remove.adhoc.net> wrote in message

news:39a467ef.35835779@forums.borland.com...

Quote
> On Mon, 21 Aug 2000 18:28:53 -0500, "Sandino Flores Moreno"
> <sflo...@fcfm.uanl.mx> wrote:

> [snip]
> >  i= sqrt( r*r +g*g +b*b )/sqrt(3.0);  /**compare this operation with to
> >obtain vectorial norm.*/
> [snip]

> Hi,

>     you have to use one of the follwing two formulas in order
> to pay attention for CRT phosphors and human eyes when
> converting to grayscale:

> Y = 0.114 * Blue + 0.587 * Green + 0.299 * Red

> or

> Y = 0.072169 * Blue  + 0.715160 * Green + 0.21267 * Red

> (second preferred)

> where Y is the luminance level.

Thanks for the formulas.

Quote

> Then you have to rebuild TColor in this manner:

TColor? Is this from something like OWL or VCL? I don't have BCB, I've got
free command line tools (BCC 5.5), so I can't use this stuff. How would that
look in plain Win32 API.

- Show quoted text -

Quote

> TColor( (int)Y * (65536 + 256 + 1) );

> Hope this helps.

> Ciao!

> Giuliano

Re:RGB to Greyscale.


On Tue, 22 Aug 2000 18:58:01 +0200, "Michael Kruglos" <mik...@inter.net.il>
wrote:

[snip]

Quote
>TColor? Is this from something like OWL or VCL? I don't have BCB, I've got
>free command line tools (BCC 5.5), so I can't use this stuff. How would that
>look in plain Win32 API.

[snip]

Ok, I've intended that you working with VCL: Ignore TColor.
For Win32 API you could try to search on SDK's
Win32 Programmer's Reference online-help for
RGBQUAD and RGBTRIPLE (I think) .

Ciao!

Giuliano

Re:RGB to Greyscale.


Quote
Giuliano <gcardin...@remove.adhoc.net> wrote in message

news:39a3a7bf.52172179@forums.borland.com...
Quote
> On Tue, 22 Aug 2000 18:58:01 +0200, "Michael Kruglos"

<mik...@inter.net.il>

Quote
> wrote:

> [snip]
> >TColor? Is this from something like OWL or VCL? I don't have BCB, I've
got
> >free command line tools (BCC 5.5), so I can't use this stuff. How would
that
> >look in plain Win32 API.
> [snip]

> Ok, I've intended that you working with VCL: Ignore TColor.
> For Win32 API you could try to search on SDK's
> Win32 Programmer's Reference online-help for
> RGBQUAD and RGBTRIPLE (I think) .

These are structures. One of 4 and second of 3 members. How can they be
connected with TColor((int)Y* (65536+256+1)); ? This TColor thingy seems to
get int, but RGBQUAD and RGBTRIPLE seem to have R, G, B, and one reserved
(for RGBQUAD). I need red, green and blue values to use them and not only Y.

Anyway thanks for helping.

Quote

> Ciao!

> Giuliano

Re:RGB to Greyscale.


Quote
"Michael Kruglos" <mik...@inter.net.il> wrote in message news:8nulhi$7f1@bornews.borland.com...
> These are structures. One of 4 and second of 3 members. How can they be
> connected with TColor((int)Y* (65536+256+1)); ? This TColor thingy seems to
> get int, but RGBQUAD and RGBTRIPLE seem to have R, G, B, and one reserved
> (for RGBQUAD). I need red, green and blue values to use them and not only Y.

You compute the Y and stuff it into the R, G and B elements.  Grayscale values
are defined by RGB(i, i, i), where i = 0 .. 255.  So even though you have 16,777,216
colors with 24-bit graphics, you only have 256 shades of gray.

--
efg

Earl F. Glynn     E-mail:  EarlGl...@att.net
Overland Park, KS  USA

efg's Computer Lab:  http://www.efg2.com/Lab

Re:RGB to Greyscale.


Now I see. Thanks.
Earl F. Glynn <EarlGl...@att.net> wrote in message
news:8numhr$7c4@bornews.borland.com...
Quote
> "Michael Kruglos" <mik...@inter.net.il> wrote in message

news:8nulhi$7f1@bornews.borland.com...
Quote

> > These are structures. One of 4 and second of 3 members. How can they be
> > connected with TColor((int)Y* (65536+256+1)); ? This TColor thingy seems
to
> > get int, but RGBQUAD and RGBTRIPLE seem to have R, G, B, and one
reserved
> > (for RGBQUAD). I need red, green and blue values to use them and not
only Y.

> You compute the Y and stuff it into the R, G and B elements.  Grayscale
values
> are defined by RGB(i, i, i), where i = 0 .. 255.  So even though you have
16,777,216
> colors with 24-bit graphics, you only have 256 shades of gray.

> --
> efg

> Earl F. Glynn     E-mail:  EarlGl...@att.net
> Overland Park, KS  USA

> efg's Computer Lab:  http://www.efg2.com/Lab

Other Threads