颜色空间转换

色彩空间转换详解

0. 结构体

typedef struct{    //RGB格式颜色
    unsigned char  red;             // [0,255]
    unsigned char  green;           // [0,255]
    unsigned char  blue;            // [0,255]
}COLOR_RGB;
typedef struct{    //YUV格式颜色   
    unsigned char Y_hue;              // [0,240]
    unsigned char U_saturation;       // [0,240]
    unsigned char V_luminance;        // [0,240]明亮度}COLOR_YUV;
typedef struct{    //LAB格式颜色    
    unsigned char L_Luminosity;         // [0,100]
    unsigned char A_ColorChannel;       // [-127,128]
    unsigned char B_ColorChannel;        // [127,128]
}COLOR_LAB;
typedef struct{    //HSL颜色格式               
    unsigned char hue;              // [0,100]
    unsigned char saturation;       // [-128,127]
    unsigned char luminance;        // [-128,127]
}COLOR_HSL

1. RGB--->YUV

void RGBtoYUV(const COLOR_RGB *Rgb, COLOR_YUV *Yuv)
{
  Yuv->Y_hue = 0.299 * Rgb->red + 0.587 * Rgb->green + 0.114 * Rgb->blue;
    Yuv->U_saturation = -0.147 * Rgb->red -0.289 * Rgb->green + 0.436 * Rgb->blue;
    Yuv->V_luminance = 0.615 * Rgb->red - 0.515 * Rgb->green - 0.100 * Rgb->blue;
}

2. RGB---->LAB(error)

void RGBtoLAB(const COLOR_RGB *Rgb, COLOR_LAB *Lab)
{
    Lab->L_Luminosity = 0.2126007 * Rgb->red + 0.7151947 * Rgb->green + 0.0722046 * Rgb->blue;
    Lab->A_ColorChannel = 0.3258962 * Rgb->red - 0.4992596 * Rgb->green + 0.1733409 * Rgb->blue + 128;
    Lab->B_ColorChannel = 0.1218128 * Rgb->red + 0.3785610 * Rgb->green - 0.5003738 * Rgb->blue + 128;
}
http://blog.sina.com.cn/s/blog_91528c150101mwbe.html 

3. LAB--->RGB

void LABtoRGB(const COLOR_LAB *Lab, COLOR_RGB *Rgb)
{
   Rgb->red = Lab->L_Luminosity + 0.0120308 * Lab->A_ColorChannel + 0.0021207 * Lab->B_ColorChannel;
   Rgb->green = Lab->L_Luminosity - 0.0035973 * Lab->A_ColorChannel - 0.0001765 * Lab->B_ColorChannel;
   Rgb->blue = Lab->L_Luminosity + 0.0002074 * Lab->A_ColorChannel - 0.0044965 * Lab->B_ColorChannel;
}

4. RGB--->HSL

#define min3v(v1, v2, v3)   ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))
#define max3v(v1, v2, v3)   ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))
void RGBtoHSL(const COLOR_RGB *Rgb, COLOR_HSL *Hsl)
{
  int h,s,l,maxVal,minVal,difVal;
    int r  = Rgb->red;
    int g  = Rgb->green;
  int b  = Rgb->blue;
    
    maxVal = max3v(r, g, b);
    minVal = min3v(r, g, b);
    
    difVal = maxVal-minVal;
    
    //计算亮度
        l = (maxVal+minVal)*240/255/2;
    
    if(maxVal == minVal)//如果r=g=b
    {
        h = 0; 
        s = 0;
    }
    else
    {
                //计算色调
        if(maxVal==r)
        {
            if(g>=b)
                h = 40*(g-b)/(difVal);
            else
                h = 40*(g-b)/(difVal) + 240;
        }
        else if(maxVal==g)
            h = 40*(b-r)/(difVal) + 80;
        else if(maxVal==b)
            h = 40*(r-g)/(difVal) + 160;
        //计算饱和度
        if(l == 0)
            s = 0;
        else if(l<=120)
            s = (difVal)*240/(maxVal+minVal);
        else
            s = (difVal)*240/(511 - (maxVal+minVal));
    }
    Hsl->hue =        (unsigned char)(((h>240)? 240 : ((h<0)?0:h)));
    Hsl->saturation = (unsigned char)(((s>240)? 240 : ((s<0)?0:s)));
    Hsl->luminance =  (unsigned char)(((l>240)? 240 : ((l<0)?0:l)));
}

//声明:以上代码是笔者参考网上资源编辑而成,侵删

转载于:https://www.cnblogs.com/Ocean-Star/p/6994678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值