RGB转LAB代码

思路:先RGB转成XYZ,再XYZ转LAB

代码:

RGB2XYZ(const int& sR,const int&sG,const int& sB,

double& X, double& Y, double& Z)
{
double R = sR/255.0;
double G = sG/255.0;
double B = sB/255.0;

double r, g, b;


if(R <= 0.04045) r = R/12.92;
else r = pow((R+0.055)/1.055,2.4);
if(G <= 0.04045) g = G/12.92;
else g = pow((G+0.055)/1.055,2.4);
if(B <= 0.04045) b = B/12.92;
else b = pow((B+0.055)/1.055,2.4);


X = r*0.4124564 + g*0.3575761 + b*0.1804375;
Y = r*0.2126729 + g*0.7151522 + b*0.0721750;
Z = r*0.0193339 + g*0.1191920 + b*0.9503041;

}

RGB2LAB(const int& sR, const int& sG, const int& sB, double& lval, double& aval, double& bval)

{
//------------------------
// sRGB to XYZ conversion
//------------------------
double X, Y, Z;
RGB2XYZ(sR, sG, sB, X, Y, Z);


//------------------------
// XYZ to LAB conversion
//------------------------
double epsilon = 0.008856; //actual CIE standard
double kappa   = 903.3; //actual CIE standard


double Xr = 0.950456; //reference white
double Yr = 1.0; //reference white
double Zr = 1.088754; //reference white


double xr = X/Xr;
double yr = Y/Yr;
double zr = Z/Zr;


double fx, fy, fz;
if(xr > epsilon) fx = pow(xr, 1.0/3.0);
else fx = (kappa*xr + 16.0)/116.0;
if(yr > epsilon) fy = pow(yr, 1.0/3.0);
else fy = (kappa*yr + 16.0)/116.0;
if(zr > epsilon) fz = pow(zr, 1.0/3.0);
else fz = (kappa*zr + 16.0)/116.0;


lval = 116.0*fy-16.0;
aval = 500.0*(fx-fy);
bval = 200.0*(fy-fz);

}


DoRGBtoLABConversion(
const unsigned int*& ubuff,
double*& lvec,
double*& avec,
double*& bvec)
{
int sz = m_width*m_height;
lvec = new double[sz];
avec = new double[sz];
bvec = new double[sz];


for( int j = 0; j < sz; j++ )
{
int r = (ubuff[j] >> 16) & 0xFF;
int g = (ubuff[j] >>  8) & 0xFF;
int b = (ubuff[j]      ) & 0xFF;


RGB2LAB( r, g, b, lvec[j], avec[j], bvec[j] );
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值