RGB、HSV色彩空间模式的互相转换
2007-11-14 11:20
435 查看
在开发有关bitmap方面的程序时,经常需要将位图的颜色在RGB和HSV色彩空间之间转换,该颜色转换由C++实现:
RGB颜色空间转换为HSV空间颜色值:
void Rgb2Hsv(float R, float G, float B, float& H, float& S, float&V)
{
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
float min, max, delta,tmp;
tmp = min(R, G);
min = min( tmp, B );
tmp = max( R, G);
max = max(tmp, B );
V = max; // v
delta = max - min;
if( max != 0 )
S = delta / max; // s
else
{
// r = g = b = 0 // s = 0, v is undefined
S = 0;
H = UNDEFINEDCOLOR;
return;
}
if( R == max )
H = ( G - B ) / delta; // between yellow & magenta
else if( G == max )
H = 2 + ( B - R ) / delta; // between cyan & yellow
else
H = 4 + ( R - G ) / delta; // between magenta & cyan
H *= 60; // degrees
if( H < 0 )
H += 360;
}
HSV颜色空间转换为RGB空间颜色值:
void Hsv2Rgb(float H, float S, float V, float &R, float &G, float &B)
{
int i;
float f, p, q, t;
if( S == 0 )
{
// achromatic (grey)
R = G = B = V;
return;
}
H /= 60; // sector 0 to 5
i = floor( H );
f = H - i; // factorial part of h
p = V * ( 1 - S );
q = V * ( 1 - S * f );
t = V * ( 1 - S * ( 1 - f ) );
switch( i )
{
case 0:
R = V;
G = t;
B = p;
break;
case 1:
R = q;
G = V;
B = p;
break;
case 2:
R = p;
G = V;
B = t;
break;
case 3:
R = p;
G = q;
B = V;
break;
case 4:
R = t;
G = p;
B = V;
break;
default: // case 5:
R = V;
G = p;
B = q;
break;
}
}
【来源】
RGB颜色空间转换为HSV空间颜色值:
void Rgb2Hsv(float R, float G, float B, float& H, float& S, float&V)
{
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
float min, max, delta,tmp;
tmp = min(R, G);
min = min( tmp, B );
tmp = max( R, G);
max = max(tmp, B );
V = max; // v
delta = max - min;
if( max != 0 )
S = delta / max; // s
else
{
// r = g = b = 0 // s = 0, v is undefined
S = 0;
H = UNDEFINEDCOLOR;
return;
}
if( R == max )
H = ( G - B ) / delta; // between yellow & magenta
else if( G == max )
H = 2 + ( B - R ) / delta; // between cyan & yellow
else
H = 4 + ( R - G ) / delta; // between magenta & cyan
H *= 60; // degrees
if( H < 0 )
H += 360;
}
HSV颜色空间转换为RGB空间颜色值:
void Hsv2Rgb(float H, float S, float V, float &R, float &G, float &B)
{
int i;
float f, p, q, t;
if( S == 0 )
{
// achromatic (grey)
R = G = B = V;
return;
}
H /= 60; // sector 0 to 5
i = floor( H );
f = H - i; // factorial part of h
p = V * ( 1 - S );
q = V * ( 1 - S * f );
t = V * ( 1 - S * ( 1 - f ) );
switch( i )
{
case 0:
R = V;
G = t;
B = p;
break;
case 1:
R = q;
G = V;
B = p;
break;
case 2:
R = p;
G = V;
B = t;
break;
case 3:
R = p;
G = q;
B = V;
break;
case 4:
R = t;
G = p;
B = V;
break;
default: // case 5:
R = V;
G = p;
B = q;
break;
}
}
【来源】
相关文章推荐
- RGB、HSV色彩空间模式的互相转换
- (zt)RGB、HSV色彩空间模式的互相转换
- Atitit rgb yuv hsv HSL 模式和 HSV(HSB) 图像色彩空间的区别
- Atitit rgb yuv hsv HSL 模式和 HSV(HSB) 图像色彩空间的区别
- RGB HSV HLS三种色彩模式转换(C语言实现)
- 基于OpenCV的RGB和HSV色彩空间相互转换C++程序
- VC编程实现色彩空间RGB与HSB(HSV)相互转换
- VC编程实现色彩空间RGB与HSB(HSV)相互转换
- 色彩模式-RGB、HSV、CMYK、灰度模式、位图模式......
- 【shaderforge学习笔记】 RGB to HSV、HSV to RGB节点(色彩空间转换节点)
- HSL和HSV色彩空间和rgb的互转
- 基于OpenCV的RGB和HSV色彩空间相互转换C++程序
- matlab实现RGB与HSV(HSB)、HSL和HSI的色彩空间互转
- CNN结构:色彩特征提取-从RGB空间到HSV空间(色彩冷暖判断)
- 【学习 OpenCV】—— 色彩空间(RGB、HSV、rgb2hsv、rgb2gray 的实现)
- RGB、Lab、YUV、HSI、HSV等颜色空间的区别
- 颜色空间变换(RGB-HSV)
- YUV RGB HSV HSI CMYK HSL等各种颜色空间
- 色彩空间模式笔记
- 颜色空间RGB与HSV(HSL)的转换