| 1 |
|
| #include <stdio.h> |
| 2 |
|
| #include <math.h> |
| 3 |
|
| |
| 4 |
|
| #define pi 3.141592653589793238462643383279 |
| 5 |
|
| |
| 6 |
|
| /*** function defs ***/ |
| 7 |
|
| void deltaE2000( double *lab1, double *lab2, double *delta_e ); |
| 8 |
|
| |
| 9 |
|
| /// Computes the CIEDE2000 color-difference between two Lab colors |
| 10 |
|
| /// Based on the article: |
| 11 |
|
| /// The CIEDE2000 Color-Difference Formula: Implementation Notes, |
| 12 |
|
| /// Supplementary Test Data, and Mathematical Observations,", G. Sharma, |
| 13 |
|
| /// W. Wu, E. N. Dalal, submitted to Color Research and Application, |
| 14 |
|
| /// January 2004. |
| 15 |
|
| /// Available at http://www.ece.rochester.edu/~/gsharma/ciede2000/ |
| 16 |
|
| /// Based on the C++ implementation by Ofir Pele, The Hebrew University of Jerusalem 2010. |
| 17 |
|
| // |
| 18 |
68 |
| void deltaE2000( double *lab1, double *lab2, double *delta_e ) |
| 19 |
|
| { |
| 20 |
68 |
| double Lstd = *lab1; |
| 21 |
68 |
| double astd = *(lab1+1); |
| 22 |
68 |
| double bstd = *(lab1+2); |
| 23 |
|
| |
| 24 |
68 |
| double Lsample = *lab2; |
| 25 |
68 |
| double asample = *(lab2+1); |
| 26 |
68 |
| double bsample = *(lab2+2); |
| 27 |
|
| |
| 28 |
68 |
| double _kL = 1.0; |
| 29 |
68 |
| double _kC = 1.0; |
| 30 |
68 |
| double _kH = 1.0; |
| 31 |
|
| |
| 32 |
68 |
| double Cabstd= sqrt(astd*astd+bstd*bstd); |
| 33 |
68 |
| double Cabsample= sqrt(asample*asample+bsample*bsample); |
| 34 |
|
| |
| 35 |
68 |
| double Cabarithmean= (Cabstd + Cabsample)/2.0; |
| 36 |
|
| |
| 37 |
68 |
| double G= 0.5*( 1.0 - sqrt( pow(Cabarithmean,7.0)/(pow(Cabarithmean,7.0) + pow(25.0,7.0)))); |
| 38 |
|
| |
| 39 |
68 |
| double apstd= (1.0+G)*astd; // aprime in paper |
| 40 |
68 |
| double apsample= (1.0+G)*asample; // aprime in paper |
| 41 |
68 |
| double Cpsample= sqrt(apsample*apsample+bsample*bsample); |
| 42 |
|
| |
| 43 |
68 |
| double Cpstd= sqrt(apstd*apstd+bstd*bstd); |
| 44 |
|
| // Compute product of chromas |
| 45 |
68 |
| double Cpprod= (Cpsample*Cpstd); |
| 46 |
|
| |
| 47 |
|
| |
| 48 |
|
| // Ensure hue is between 0 and 2pi |
| 49 |
68 |
| double hpstd= atan2(bstd,apstd); |
| 50 |
68 |
▶ | if (hpstd<0) hpstd+= 2.0*pi; // rollover ones that come -ve |
Branch: 1 branch
| Branch | True | False | Total |
| if (hpstd<0) hpstd+= 2.0*pi; // rollover ones that come -ve |
32 |
36 |
68 |
|
| 51 |
|
| |
| 52 |
68 |
| double hpsample= atan2(bsample,apsample); |
| 53 |
68 |
▶ | if (hpsample<0) hpsample+= 2.0*pi; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hpsample<0) hpsample+= 2.0*pi; |
36 |
32 |
68 |
|
| 54 |
68 |
▶ | if ( (fabs(apsample)+fabs(bsample))==0.0) hpsample= 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (fabs(apsample)+fabs(bsample))==0.0) hpsample= 0.0; |
2 |
66 |
68 |
|
| 55 |
|
| |
| 56 |
68 |
| double dL= (Lsample-Lstd); |
| 57 |
68 |
| double dC= (Cpsample-Cpstd); |
| 58 |
|
| |
| 59 |
|
| // Computation of hue difference |
| 60 |
68 |
| double dhp= (hpsample-hpstd); |
| 61 |
68 |
▶ | if (dhp>pi) dhp-= 2.0*pi; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dhp>pi) dhp-= 2.0*pi; |
8 |
60 |
68 |
|
| 62 |
68 |
▶ | if (dhp<-pi) dhp+= 2.0*pi; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dhp<-pi) dhp+= 2.0*pi; |
4 |
64 |
68 |
|
| 63 |
|
| // set chroma difference to zero if the product of chromas is zero |
| 64 |
68 |
▶ | if (Cpprod == 0.0) dhp= 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (Cpprod == 0.0) dhp= 0.0; |
4 |
64 |
68 |
|
| 65 |
|
| |
| 66 |
|
| // Note that the defining equations actually need |
| 67 |
|
| // signed Hue and chroma differences which is different |
| 68 |
|
| // from prior color difference formulae |
| 69 |
|
| |
| 70 |
68 |
| double dH= 2.0*sqrt(Cpprod)*sin(dhp/2.0); |
| 71 |
|
| //%dH2 = 4*Cpprod.*(sin(dhp/2)).^2; |
| 72 |
|
| |
| 73 |
|
| // weighting functions |
| 74 |
68 |
| double Lp= (Lsample+Lstd)/2.0; |
| 75 |
68 |
| double Cp= (Cpstd+Cpsample)/2.0; |
| 76 |
|
| |
| 77 |
|
| // Average Hue Computation |
| 78 |
|
| // This is equivalent to that in the paper but simpler programmatically. |
| 79 |
|
| // Note average hue is computed in radians and converted to degrees only |
| 80 |
|
| // where needed |
| 81 |
68 |
| double hp= (hpstd+hpsample)/2.0; |
| 82 |
|
| // Identify positions for which abs hue diff exceeds 180 degrees |
| 83 |
68 |
▶ | if ( fabs(hpstd-hpsample) > pi ) hp-= pi; |
Branch: 1 branch
| Branch | True | False | Total |
| if ( fabs(hpstd-hpsample) > pi ) hp-= pi; |
12 |
56 |
68 |
|
| 84 |
|
| // rollover ones that come -ve |
| 85 |
68 |
▶ | if (hp<0) hp+= 2.0*pi; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hp<0) hp+= 2.0*pi; |
6 |
62 |
68 |
|
| 86 |
|
| |
| 87 |
|
| // Check if one of the chroma values is zero, in which case set |
| 88 |
|
| // mean hue to the sum which is equivalent to other value |
| 89 |
68 |
▶ | if (Cpprod==0.0) hp= hpsample+hpstd; |
Branch: 1 branch
| Branch | True | False | Total |
| if (Cpprod==0.0) hp= hpsample+hpstd; |
4 |
64 |
68 |
|
| 90 |
|
| |
| 91 |
68 |
| double Lpm502= (Lp-50.0)*(Lp-50.0);; |
| 92 |
68 |
| double Sl= 1.0+0.015*Lpm502/sqrt(20.0+Lpm502); |
| 93 |
68 |
| double Sc= 1.0+0.045*Cp; |
| 94 |
68 |
| double T= 1.0 - 0.17*cos(hp - pi/6.0) + 0.24*cos(2.0*hp) + 0.32*cos(3.0*hp+pi/30.0) - 0.20*cos(4.0*hp-63.0*pi/180.0); |
| 95 |
68 |
| double Sh= 1.0 + 0.015*Cp*T; |
| 96 |
68 |
| double delthetarad= (30.0*pi/180.0)*exp(- pow(( (180.0/pi*hp-275.0)/25.0),2.0)); |
| 97 |
68 |
| double Rc= 2.0*sqrt(pow(Cp,7.0)/(pow(Cp,7.0) + pow(25.0,7.0))); |
| 98 |
68 |
| double RT= -sin(2.0*delthetarad)*Rc; |
| 99 |
|
| |
| 100 |
|
| // The CIE 00 color difference |
| 101 |
68 |
| *delta_e = sqrt( pow((dL/Sl),2.0) + pow((dC/Sc),2.0) + pow((dH/Sh),2.0) + RT*(dC/Sc)*(dH/Sh) ); |
| 102 |
68 |
| } |