Convert to BW
-
To do that, you just need to de-saturate the image. The RGB colour-space does not easily lend itself to this operation, and the HSL colour model is far more useful here. In an entirely non optimized method, you would: Repeat loop for all pixels in source image (1) convert pixel RGB to HSL (2) drop the S value to 0 (3) convert back to RGB (4) dump the converted pixel back Here's some routines for colour-space conversion:
// Input: h, s, v in range [0..1]
// Outputs: r, g, b in range [0..1]
void hsvToRgb(float h, float s, float v, float *r, float *g, float *b)
{
int i;
float aa, bb, cc, f;if (s == 0.0) // greyscale \*r = \*g = \*b = v; else { if (h == 1.0) h = 0.0; h \*= 6.0; i = floor(h); f = h - (float)i; aa = v \* (1.0 - s); bb = v \* (1.0 - (s \* f)); cc = v \* (1.0 - (s \* (1.0 - f))); switch(i) { case 0: \*r = v; \*g = cc; \*b = aa; break; case 1: \*r = bb; \*g = v; \*b = aa; break; case 2: \*r = aa; \*g = v; \*b = cc; break; case 3: \*r = aa; \*g = bb; \*b = v; break; case 4: \*r = cc; \*g = aa; \*b = v; break; case 5: \*r = v; \*g = aa; \*b = bb; break; } }
}
#define myMin(a,b) (a
#define myMax(a,b) (a>b?a:b)
#define noHue 0// Inputs: r, g, b in range [0..1]
// Outputs: h, s, v in range [0..1]
void rgbToHsv(float r, float g, float b, float *h, float *s, float *v)
{
float max = myMax(r, myMax(g, b)), min = myMin(r, myMin(g, b));
float delta = max - min;\*v = max; if (max != 0.0) \*s = delta / max; else \*s = 0.0; if (\*s == 0.0) \*h = noHue; else { if (r == max) \*h = (g - b) / delta; else if (g == max) \*h = 2 + (b - r) / delta; else if (b == max) \*h = 4 + (r - g) / delta; \*h \*= 60.0; if (\*h < 0) \*h += 360.0; \*h /= 360.0; }
}
I'll leave it to you to make them work with RGB values in the range [0..255]
-
To do that, you just need to de-saturate the image. The RGB colour-space does not easily lend itself to this operation, and the HSL colour model is far more useful here. In an entirely non optimized method, you would: Repeat loop for all pixels in source image (1) convert pixel RGB to HSL (2) drop the S value to 0 (3) convert back to RGB (4) dump the converted pixel back Here's some routines for colour-space conversion:
// Input: h, s, v in range [0..1]
// Outputs: r, g, b in range [0..1]
void hsvToRgb(float h, float s, float v, float *r, float *g, float *b)
{
int i;
float aa, bb, cc, f;if (s == 0.0) // greyscale \*r = \*g = \*b = v; else { if (h == 1.0) h = 0.0; h \*= 6.0; i = floor(h); f = h - (float)i; aa = v \* (1.0 - s); bb = v \* (1.0 - (s \* f)); cc = v \* (1.0 - (s \* (1.0 - f))); switch(i) { case 0: \*r = v; \*g = cc; \*b = aa; break; case 1: \*r = bb; \*g = v; \*b = aa; break; case 2: \*r = aa; \*g = v; \*b = cc; break; case 3: \*r = aa; \*g = bb; \*b = v; break; case 4: \*r = cc; \*g = aa; \*b = v; break; case 5: \*r = v; \*g = aa; \*b = bb; break; } }
}
#define myMin(a,b) (a
#define myMax(a,b) (a>b?a:b)
#define noHue 0// Inputs: r, g, b in range [0..1]
// Outputs: h, s, v in range [0..1]
void rgbToHsv(float r, float g, float b, float *h, float *s, float *v)
{
float max = myMax(r, myMax(g, b)), min = myMin(r, myMin(g, b));
float delta = max - min;\*v = max; if (max != 0.0) \*s = delta / max; else \*s = 0.0; if (\*s == 0.0) \*h = noHue; else { if (r == max) \*h = (g - b) / delta; else if (g == max) \*h = 2 + (b - r) / delta; else if (b == max) \*h = 4 + (r - g) / delta; \*h \*= 60.0; if (\*h < 0) \*h += 360.0; \*h /= 360.0; }
}
I'll leave it to you to make them work with RGB values in the range [0..255]
enhzflep wrote:
Here's some routines for colour-space conversion:
These functions does the same, right? ColorRGBToHLS ColorHLSToRGB
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
enhzflep wrote:
Here's some routines for colour-space conversion:
These functions does the same, right? ColorRGBToHLS ColorHLSToRGB
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
:-\ After looking at: http://msdn.microsoft.com/en-us/library/bb773851(VS.85).aspx[^] It would certainly appear so. In fact, they'd be even better, since they deal with WORDs instead of floats. That'll teach me for using a 11 year old API help file.. Thanks for the better solution.
-
:-\ After looking at: http://msdn.microsoft.com/en-us/library/bb773851(VS.85).aspx[^] It would certainly appear so. In fact, they'd be even better, since they deal with WORDs instead of floats. That'll teach me for using a 11 year old API help file.. Thanks for the better solution.
enhzflep wrote:
That'll teach me for using a 11 year old API help file..
:)
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com