Java实现的几种色差公式

最近做色差计算,需要用到CIE 1976Lab、CMC(l:c)、CIE DE 2000三个色差公式,需要用Java实现,具体的公式网上都有,这里就不再赘述,下面直接贴代码:

1.CIE 1976Lab

/**
*  CIE 1976Lab
* */
public static double CIE_1976(double L1,double a1,double b1,double L2,double a2,double b2){
	L = Math.abs(L2-L1);
	a = Math.abs(a2-a1);
	b = Math.abs(b2-b1);
	double E = Math.sqrt(Math.pow(L,2)+Math.pow(a,2)+Math.pow(b,2));
	return E;
}

2.CMC(l:c)

/**
* CMC (1.4:1)
* */
public static double CMC(double L1,double a1,double b1,double C1,double L2,double a2,double b2,double C2){
	//计算a' C' h'
	double L_1 = L1;
	double C_avg = (double)(C1+C2)/2;
	double G = 0.5*(1-Math.sqrt((double)(Math.pow(C_avg, 7))/(Math.pow(C_avg, 7)+Math.pow(25, 7))));
	double a_1 = (1+G)*a1;
	double b_1 = b1;
	double C_ab1 = Math.sqrt(Math.pow(a_1, 2)+Math.pow(b_1, 2));
	double h_ab1 = Math.atan((double)b_1/a_1);

	double L_2 = L2;
	double a_2 = (1+G)*a2;
	double b_2 = b2;
	double C_ab2 = Math.sqrt(Math.pow(a_2, 2)+Math.pow(b_2, 2));
	double h_ab2 = Math.atan((double)b_2/a_2);

	//计算差值 L’ Cab’ Hab’
	double L_diff = Math.abs(L_1-L_2);
	double C_diff = Math.abs(C_ab1-C_ab2);
	double h_diff = Math.abs(h_ab1-h_ab2);
	double H_diff = 2*Math.sqrt(C_ab1*C_ab2)*Math.sin((double)((h_diff)/2));
	    	
	//计算S_L
	double S_L = 0;
	if (L_1 >= 16) {
		S_L = (double)0.040975*L_1/(1+0.01765*L_1);
	}else {
		S_L = 0.511;
	}
	    	
	//计算S_C
	double S_C = ((double)0.0638*C_ab1/(1+0.0131*C_ab1)) + 0.638;
	    	
	//计算S_H
	double F = Math.sqrt((Math.pow(C_ab1, 4))/(Math.pow(C_ab1, 4)+1900));
	double T = 0;
	if (h_ab1 > 345 | h_ab1 < 164) {
		T = 0.36 + Math.abs(0.4*Math.cos(h_ab1 + 35));
	}else if (h_ab1 >= 164 & h_ab1 <= 345) {
		T = 0.56 + Math.abs(0.2*Math.cos(h_ab1 + 168));
	}
	double S_H = S_C*(F*T + 1 - F);
	    	
	//计算色差
	double l = 1.4;
	double c = 1;
	double E = Math.sqrt(Math.pow((double)L_diff/(l*S_L), 2)+Math.pow((double)C_diff/(c*S_C), 2)+Math.pow((double)H_diff/S_H, 2));
	return E;
}

3.CIE DE2000

/**
* CIE DE 2000
* */
public static double CIE_2000(double L1,double a1,double b1,double C1,double L2,double a2,double b2,double C2){
	//计算a' C' h'
	double L_1 = L1;
	double C_avg = (double)(C1+C2)/2;
	double G = 0.5*(1-Math.sqrt((double)(Math.pow(C_avg, 7))/(Math.pow(C_avg, 7)+Math.pow(25, 7))));
	double a_1 = (1+G)*a1;
	double b_1 = b1;
	double C_ab1 = Math.sqrt(Math.pow(a_1, 2)+Math.pow(b_1, 2));
	double h_ab1 = Math.atan((double)b_1/a_1);
	    	
	double L_2 = L2;
	double a_2 = (1+G)*a2;
	double b_2 = b2;
	double C_ab2 = Math.sqrt(Math.pow(a_2, 2)+Math.pow(b_2, 2));
	double h_ab2 = Math.atan((double)b_2/a_2);

	//计算差值 L’ Cab’ Hab’
	double L_diff = Math.abs(L_1-L_2);
	double C_diff = Math.abs(C_ab1-C_ab2);
	double h_diff = Math.abs(h_ab1-h_ab2);
	double H_diff = 2*Math.sqrt(C_ab1*C_ab2)*Math.sin((double)((h_diff)/2));
	    	
	//计算Sl Sc Sh RT RC
	double L_avg = (double)(L_1+L_2)/2;
	double C_ab_avg = (double)(C_ab1+C_ab2)/2;
	double h_ab_avg = (double)(h_ab1+h_ab2)/2;
	double T = 1 - 0.17*Math.cos(h_ab_avg - 30) + 0.24*Math.cos(2*h_ab_avg) + 0.32*Math.cos(3*h_ab_avg+6) - 0.20*Math.cos(4*h_ab_avg-63);
	double S_L = 1+((double)(0.015*Math.pow(L_avg-50, 2))/(Math.sqrt(20+Math.pow(L_avg-50, 2))));
	double S_C = 1+0.045*C_ab_avg;
	double S_H = 1+0.015*C_ab_avg*T;
	double RC = 2*(Math.sqrt((Math.pow(C_avg, 7))/(Math.pow(C_avg, 7)+Math.pow(25, 7))));
	double angle = 30*Math.exp(-Math.pow((double)(h_ab_avg-275)/(25),2));
	double RT = -Math.sin(2*angle)*RC;
	    	
	//选取KL KC KH,符合标准观测环境
	double K_L = 1;
	double K_C = 1;
	double K_H = 1; 
	    	
	//色差计算
	double E = Math.sqrt(Math.pow((double)L_diff/(K_L*S_L), 2)+Math.pow((double)C_diff/(K_C*S_C), 2)+
	    		Math.pow((double)H_diff/(K_H*S_H), 2)+RT*(C_diff/(K_C*S_C))*(H_diff/(K_H*S_H)));
	return E;
}

猜你喜欢

转载自blog.csdn.net/xiaoxun2802/article/details/79515539