图像从RGB域转到LAB域

文章目录


前言

在图像处理中经常会涉及到图像在不同颜色域之间的转换,本文主要是对图像从RGB域转LAB域的简单描述,并附上C代码


一、RGB域和LAB域的区别?

RGB域是由红色通道(R)、绿色通道(G)和蓝色通道(B)组成的。LAB颜色空间中的L分量表示像素的亮度,取值范围是[0,100],表示从纯黑到纯白;a表示从红色到绿色,取值范围是[127,-128];b表示从黄色到蓝色,取值范围是[127,-128]。

二、RGB域转LAB域

1.RGB域转LAB域公式

RGB空间不能直接转到LAB空间,需要借助XYZ空间,公式如下,其中X/0.950456和Z/1.088754的原因是X=0.412453R+0.357580G+0.180423B,其中0.412453+0.357580+0.180423=0.950456,系数和不等于1,R/G/B的取值范围为[0,255],X的取值范围也是[0,255],等比修改其系数,可以使其系数和等于1。

 temp_R=(R/255) \\temp_G=(G/255) \\temp_B=(B/255)\\ \left\{\begin{matrix} temp_R=((temp_R+0.055)/1.055)^{2.4},temp_R>0.04045 \\ temp_R=temp_R/12.92.temp_R\leqslant 0.04045 \end{matrix}\right.\\ \left\{\begin{matrix} temp_G=((temp_G+0.055)/1.055)^{2.4},temp_G>0.04045 \\ temp_G=temp_G/12.92.temp_G\leqslant 0.04045 \end{matrix}\right.\\\left\{\begin{matrix} temp_B=((temp_B+0.055)/1.055)^{2.4},temp_B>0.04045 \\ temp_B=temp_B/12.92.temp_B\leqslant 0.04045 \end{matrix}\right.\\ \begin{bmatrix} X\\ Y\\ Z \end{bmatrix}=\begin{bmatrix} 0.412453 &0.357580 &0.180423 \\ 0.212671 &0.715160 & 0.072169\\ 0.019334& 0.119193 & 0.950227 \end{bmatrix}\begin{bmatrix} temp_R\\ temp_G\\ temp_B \end{bmatrix}\\ X=X/0.950456\\ Y=Y/1\\ Z=Z/1.088754\\ f(t)=\left\{\begin{matrix} t^{1/3} & t>(\frac{6}{29})^{3}\\ \frac{1}{3}(\frac{29}{6})^{2}t+\frac{4}{29} & t\leq(\frac{6}{29}) ^{3} \end{matrix}\right.\\ L=116f(Y)-16\\ a=500(f(X)-f(Y))\\ b=200(f(Y)-f(Z))

2.RGB域转LAB域代码

代码如下:

 

#include<math.h>

typedef struct tag_ST_LAB
{
    float L;
    float a;
    float b;
}ST_LAB;

typedef struct tag_ST_RGB
{
    float R;
    float G;
    float B;
}ST_RGB;

float Gamma(float x)
{
    return x > 0.04045f ? pow((x + 0.055f) / 1.055f, 2.4f) : x / 12.92f;
}

ST_LAB RGB2Lab(ST_RGB stRGB)
{
    float fR = Gamma(stRGB.R / 255.f);
    float fG = Gamma(stRGB.G / 255.f);
    float fB = Gamma(stRGB.B / 255.f);

    float fX = 0.412453f * fR + 0.357580f * fG + 0.180423f * fB;
    float fX = 0.212671f * fR + 0.715160f * fG + 0.072169f * fB;
    float fX = 0.019334f * fR + 0.119193f * fG + 0.950227f * fB;

    fX /= 0.950456f;
    fZ /= 1.088754f;

    float fXTemp = fX > 0.008856f ? pow(fX, 1.f / 3) : (7.787f * fX + 0.137931f);
    float fYTemp = fY > 0.008856f ? pow(fX, 1.f / 3) : (7.787f * fX + 0.137931f);
    float fZTemp = fZ > 0.008856f ? pow(fX, 1.f / 3) : (7.787f * fX + 0.137931f);

    ST_LAB stLab;
    stLab.L = 116 * fYTemp - 16;
    stLab.a = 500 * (fXTemp - fYTemp);
    stLab.b = 200 * (fYTemp - fZTemp);

    return stLab;

}

int main()
{
    ST_RGB stRGB;
    stRGB.R = 0.4768;
    stRGB.G = 0.3470;
    stEGB.B = 103.4039;
    ST_LAB stLab;
    stLab = RGB2Lab(stRGB);
    return 0;
}


总结

 综上是RGB空间转LAB空间的简单介绍和相应的C代码。

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值