RGB是非常常用的颜色模型,然而使用RGB实现颜色和亮度这两个正交分量的同时编码,显然是非常困难的。除了RGB,此外还有HSV/SHB,HSL等颜色编码方案满足不同的需求,其中,HSV非常契合实现颜色和亮度的同时编码的需求。参考:http://colorizer.org/
HSV/SHB模型中,H代表颜色,取值范围为0-360,相当于一个时钟转一圈,0和360对应同一种颜色,S为颜色饱和度,describes how white the color is。V为亮度。S,V取值从
0−100
。
在实际应用中,根据深度信息直接编码S,根据亮度直接编码V,饱和度直接取100。由于S代表的颜色从0-360循环,从0-360范围内存在红-蓝-红的过程,因此考虑只取红-蓝这一段,大概对应着0-256这个范围。然后需要将HSV颜色转换为RGB颜色进行图像显示,这里给出一个已经调试通过的代码。基本原理是:
1,亮度与RGB能达到的最大值成正比,为2.55倍关系。
2,饱和度从100每减一,RGB能达到的最小值从0到亮度设定的最大值线性增大1/100份.
3,根据H的变化分为6种情况,在每一种情况中,对RBG中一个参量在RGB能达到的最小值和最大值间进行线性变换(增大或减小).
void HSVtoRGB(unsigned char *r, unsigned char *g, unsigned char *b, int h, int s, int v)
{
// convert from HSV/HSB to RGB color
// R,G,B from 0-255, H from 0-260, S,V from 0-100
// ref http://colorizer.org/
// The hue (H) of a color refers to which pure color it resembles
// The saturation (S) of a color describes how white the color is
// The value (V) of a color, also called its lightness, describes how dark the color is
int i;
float RGB_min, RGB_max;
RGB_max = v*2.55f;
RGB_min = RGB_max*(100 - s)/ 100.0f;
i = h / 60;
int difs = h % 60; // factorial part of h
// RGB adjustment amount by hue
float RGB_Adj = (RGB_max - RGB_min)*difs / 60.0f;
switch (i) {
case 0:
*r = RGB_max;
*g = RGB_min + RGB_Adj;
*b = RGB_min;
break;
case 1:
*r = RGB_max - RGB_Adj;
*g = RGB_max;
*b = RGB_min;
break;
case 2:
*r = RGB_min;
*g = RGB_max;
*b = RGB_min + RGB_Adj;
break;
case 3:
*r = RGB_min;
*g = RGB_max - RGB_Adj;
*b = RGB_max;
break;
case 4:
*r = RGB_min + RGB_Adj;
*g = RGB_min;
*b = RGB_max;
break;
default: // case 5:
*r = RGB_max;
*g = RGB_min;
*b = RGB_max - RGB_Adj;
break;
}
}
转载来源:http://blog.youkuaiyun.com/u013701860/article/details/51997932