CIEDE2000色差公式主要对CIE94公式做了如下几项修正:
重新标定近中性区域的a*轴,以改善中性色的预测性能;
将CIE94公式中的明度权重函数修改为近似V形函数;
在色相权重函数中考虑了色相角,以体现色相容限随颜色的色相而变化的事实;
包含了与BFD和Leeds色差公式中类似的椭圆选择选项,以反映在蓝色区域的色差容限椭圆不指向中心点的现象;
CIEDE2000色差公式如下:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define M_PI (3.14159265)
//彩度计算
static double CaiDu(double a, double b)
{
double Cab = 0;
Cab = pow(a * a + b * b, 0.5);
return Cab;
}
//色调角计算
static double SeDiaoJiao(double a, double b)
{
double h = 0;
double hab = 0;
h = (180 / M_PI) * atan(b / a); //有正有负
if (a > 0 && b > 0)
{
hab = h;
}
else if (a < 0 && b>0)
{
hab = 180 + h;
}
else if (a < 0 && b < 0)
{
hab = 180 + h;
}
else //a>0&&b<0
{
hab = 360 + h;
}
return hab;
}
double DeltaE2000(double Lab1[3], double Lab2[3])
{
double E00 = 0; //CIEDE2000色差E00
double LL1, LL2, aa1, aa2, bb1, bb2; //声明L' a' b' (1,2)
double delta_LL, delta_CC, delta_hh, delta_HH; // 第二部的四个量
double kL, kC, kH;
double RT = 0; //旋转函数RT
double G = 0; //G表示CIELab 颜色空间a轴的调整因子,是彩度的函数.
double mean_Cab = 0; //两个样品彩度的算术平均值
double SL, SC, SH, T;
double L1 = Lab1[0];
double a1 = Lab1[1];
double b1 = Lab1[2];
double L2 = Lab2[0];
double a2 = Lab2[1];
double b2 = Lab2[2];
//------------------------------------------
kL = 1;
kC = 1;
kH = 1;
//------------------------------------------
mean_Cab = (CaiDu(a1, b1) + CaiDu(a2, b2)) / 2;
double mean_Cab_pow7 = pow(mean_Cab, 7); //两彩度平均值的7次方
G = 0.5*(1 - pow(mean_Cab_pow7 / (mean_Cab_pow7 + pow(25, 7)), 0.5));
LL1 = L1;
aa1 = a1 * (1 + G);
bb1 = b1;
LL2 = L2;
aa2 = a2 * (1 + G);
bb2 = b2;
double CC1, CC2; //两样本的彩度值
CC1 = CaiDu(aa1, bb1);
CC2 = CaiDu(aa2, bb2);
double hh1, hh2; //两样本的色调角
hh1 = SeDiaoJiao(aa1, bb1);
hh2 = SeDiaoJiao(aa2, bb2);
delta_LL = LL1 - LL2;
delta_CC = CC1 - CC2;
delta_hh = SeDiaoJiao(aa1, bb1) - SeDiaoJiao(aa2, bb2);
delta_HH = 2 * sin(M_PI*delta_hh / 360) * pow(CC1 * CC2, 0.5);
//-------第三步--------------
//计算公式中的加权函数SL,SC,SH,T
double mean_LL = (LL1 + LL2) / 2;
double mean_CC = (CC1 + CC2) / 2;
double mean_hh = (hh1 + hh2) / 2;
SL = 1 + 0.015 * pow(mean_LL - 50, 2) / pow(20 + pow(mean_LL - 50, 2), 0.5);
SC = 1 + 0.045 * mean_CC;
T = 1 - 0.17 * cos((mean_hh - 30) * M_PI / 180) + 0.24 * cos((2 * mean_hh) * M_PI / 180)
+ 0.32 * cos((3 * mean_hh + 6) * M_PI / 180) - 0.2 * cos((4 * mean_hh - 63) * M_PI / 180);
SH = 1 + 0.015 * mean_CC * T;
//------第四步--------
//计算公式中的RT
double mean_CC_pow7 = pow(mean_CC, 7);
double RC = 2 * pow(mean_CC_pow7 / (mean_CC_pow7 + pow(25, 7)), 0.5);
double delta_xita = 30 * exp(-pow((mean_hh - 275) / 25, 2)); //△θ 以°为单位
RT = -sin((2 * delta_xita) * M_PI / 180) * RC;
double L_item, C_item, H_item;
L_item = delta_LL / (kL * SL);
C_item = delta_CC / (kC * SC);
H_item = delta_HH / (kH * SH);
E00 = pow(L_item * L_item + C_item * C_item + H_item * H_item + RT * C_item * H_item, 0.5); return E00;
}
int main()
{
double lab1[3] = {10, 20, 30};
double lab2[3] = {10, 30, 20};
double e00 = DeltaE2000(lab1, lab2);
printf("e2000=%f", e00);
return 0;
}