Implementazione di CIEDE2000 in Prolog
| Numero di visite | 440 |
|---|---|
| Numero di file visualizzati | 199 + 371 |
Questa pagina presenta unโimplementazione di riferimento della formula della differenza cromatica CIEDE2000 in Prolog. Se si desidera ottenere una corrispondenza esatta con le implementazioni di terze parti fino a 10 cifre decimali, potrebbe essere necessario apportare alcune modifiche al codice sorgente, in particolare (de)commentando alcune righe, che possono essere applicate automaticamente tramite il link sottostante.
La funzione ΔE2000 in Prolog
Consideriamo la piรน comune e accademica (Sharma, 2005) delle due formulazioni.
% This function written in Prolog is not affiliated with the CIE (International Commission on Illumination),
% and is released into the public domain. It is provided "as is" without any warranty, express or implied.
% The classic CIE ฮE2000 implementation, which operates on two L*a*b* colors, and returns their difference.
% "l" ranges from 0 to 100, while "a" and "b" are unbounded and commonly clamped to the range of -128 to 127.
ciede_2000(L1, A1, B1, L2, A2, B2, DeltaE2000) :-
% Working in Prolog with the CIEDE2000 color-difference formula.
% K_L, K_C, K_H are parametric factors to be adjusted according to
% different viewing parameters such as textures, backgrounds...
K_L is 1.0,
K_C is 1.0,
K_H is 1.0,
Pi_1 is 3.14159265358979323846,
Pi_3 is 1.04719755119659774615,
% 1. Compute chroma magnitudes ... a and b usually range from -128 to +127
A1_sq is A1 * A1,
B1_sq is B1 * B1,
C_orig_1 is sqrt(A1_sq + B1_sq),
A2_sq is A2 * A2,
B2_sq is B2 * B2,
C_orig_2 is sqrt(A2_sq + B2_sq),
% 2. Compute chroma mean and apply G compensation
C_avg is 0.5 * (C_orig_1 + C_orig_2),
C_avg_3 is C_avg * C_avg * C_avg,
C_avg_7 is C_avg_3 * C_avg_3 * C_avg,
G_denom is C_avg_7 + 6103515625.0,
G_ratio is C_avg_7 / G_denom,
G_sqrt is sqrt(G_ratio),
G_factor is 1.0 + 0.5 * (1.0 - G_sqrt),
% 3. Apply G correction to a components, compute corrected chroma
A1_prime is A1 * G_factor,
C1_prime_sq is A1_prime * A1_prime + B1 * B1,
C1_prime is sqrt(C1_prime_sq),
A2_prime is A2 * G_factor,
C2_prime_sq is A2_prime * A2_prime + B2 * B2,
C2_prime is sqrt(C2_prime_sq),
% 4. Compute hue angles in radians, adjust for negatives and wrap
H1_raw is atan2(B1, A1_prime),
H2_raw is atan2(B2, A2_prime),
(H1_raw < 0.0 -> H1_adj is H1_raw + 2.0 * Pi_1 ; H1_adj = H1_raw),
(H2_raw < 0.0 -> H2_adj is H2_raw + 2.0 * Pi_1 ; H2_adj = H2_raw),
Delta_h is abs(H1_adj - H2_adj),
H_mean_raw is 0.5 * (H1_adj + H2_adj),
H_diff_raw is 0.5 * (H2_adj - H1_adj),
% Check if hue mean wraps around pi (180 deg)
Wrap_dist is abs(Pi_1 - Delta_h),
(1.0e-14 < Wrap_dist, Pi_1 < Delta_h -> Hue_wrap = 1.0 ; Hue_wrap = 0),
H_diff is H_diff_raw + Hue_wrap * Pi_1,
% ๐ Sharmaโs formulation doesnโt use the next line, but the three after it,
% and these two variants differ by ยฑ0.0003 on the final color differences.
H_mean is H_mean_raw + Hue_wrap * Pi_1,
% (Hue_wrap =:= 1, H_mean_raw < Pi_1 -> H_mean_hi = Pi_1 ; H_mean_hi = 0.0),
% (Hue_wrap =:= 1, H_mean_hi =:= 0.0 -> H_mean_lo = Pi_1 ; H_mean_lo = 0.0),
% H_mean is H_mean_raw + H_mean_hi - H_mean_lo,
% 5. Compute hue rotation correction factor R_T
C_bar is 0.5 * (C1_prime + C2_prime),
C_bar_3 is C_bar * C_bar * C_bar,
C_bar_7 is C_bar_3 * C_bar_3 * C_bar,
Rc_denom is C_bar_7 + 6103515625.0,
R_C is sqrt(C_bar_7 / Rc_denom),
Theta is 36.0 * H_mean - 55.0 * Pi_1,
Theta_denom is -25.0 * Pi_1 * Pi_1,
Exp_argument is Theta * Theta / Theta_denom,
Exp_term is exp(Exp_argument),
Delta_theta is Pi_3 * Exp_term,
Sin_term is sin(Delta_theta),
% Rotation factor ... cross-effect between chroma and hue
R_T is -2.0 * R_C * Sin_term,
% 6. Compute lightness term ... L nominally ranges from 0 to 100
L_avg is 0.5 * (L1 + L2),
L_delta_sq is (L_avg - 50.0) * (L_avg - 50.0),
L_delta is L2 - L1,
% Adaptation to the non-linearity of light perception ... S_L
S_l_num is 0.015 * L_delta_sq,
S_l_denom is sqrt(20.0 + L_delta_sq),
S_L is 1.0 + S_l_num / S_l_denom,
L_term is L_delta / (K_L * S_L),
% 7. Compute chroma-related trig terms and factor T
Trig_1 is 0.17 * sin(H_mean + Pi_3),
Trig_2 is 0.24 * sin(2.0 * H_mean + 0.5 * Pi_1),
Trig_3 is 0.32 * sin(3.0 * H_mean + 1.6 * Pi_3),
Trig_4 is 0.2 * sin(4.0 * H_mean + 0.15 * Pi_1),
T is 1.0 - Trig_1 + Trig_2 + Trig_3 - Trig_4,
C_sum is C1_prime + C2_prime,
C_product is C1_prime * C2_prime,
C_geo_mean is sqrt(C_product),
% 8. Compute hue difference and scaling factor S_H
Sin_h_diff is sin(H_diff),
S_H is 1.0 + 0.0075 * C_sum * T,
H_term is 2.0 * C_geo_mean * Sin_h_diff / (K_H * S_H),
% 9. Compute chroma difference and scaling factor S_C
C_delta is C2_prime - C1_prime,
S_C is 1.0 + 0.0225 * C_sum,
C_term is C_delta / (K_C * S_C),
% 10. Combine lightness, chroma, hue, and interaction terms
L_part is L_term * L_term,
C_part is C_term * C_term,
H_part is H_term * H_term,
Interaction is C_term * H_term * R_T,
Delta_e_squared is L_part + C_part + H_part + Interaction,
DeltaE2000 is sqrt(Delta_e_squared).
% GitHub Project : https://github.com/michel-leonard/ciede2000-color-matching
% Online Tests : https://michel-leonard.github.io/ciede2000-color-matching
% L1 = 14.8 a1 = 33.5 b1 = 2.4
% L2 = 16.6 a2 = 38.1 b2 = -2.7
% CIE ฮE00 = 3.6462011992 (Bruce Lindbloom, Netflixโs VMAF, ...)
% CIE ฮE00 = 3.6461873926 (Gaurav Sharma, OpenJDK, ...)
% Deviation between implementations โ 1.4e-5
% See the source code comments for easy switching between these two widely used ฮE*00 implementation variants.Precisione e affidabilitร del codice sorgente
La differenza tra le formulazioni di Sharma e Lindbloom non supera mai ยฑ0,0003 sul ฮE2000 finale, il che corrisponde alla differenza abituale misurata tra due implementazioni a 32 bit ed รจ impercettibile a occhio umano. Le nostre implementazioni a 64 bit, tutte coerenti tra loro, garantiscono almeno 10 cifre decimali corrette, quindi la scelta di una formulazione rispetto a unโaltra dipende principalmente dallโinteroperabilitร desiderata. La formulazione che appare di default in questa pagina รจ la piรน comunemente utilizzata (il suo micro-vantaggio รจ che si basa sulla comunitร ed รจ piรน leggera del suo analogo quando viene vettorializzata).
โ Se trovate un commento nel codice sorgente che non corrisponde a unโaltra lingua, informate lโautore del sito, che studierร il vostro suggerimento e lo incorporerร nel codice sorgente.
Come si convertono i colori RGB in L*a*b*?
Andate alla pagina AWK, C, Dart, Java, JavaScript, Kotlin, Lua, PHP, Python, Ruby o Rust dove tale convertitore (che utilizza lโilluminante D65) รจ giร implementato in aggiunta alla funzione di confronto dei colori.
Intervalli di valori in CIELAB e interpretazione del ฮE2000
Nello spazio colore CIELAB, la componente L* rappresenta la luminositร e varia tipicamente da 0 (nero) a 100 (bianco). Le componenti a* e b* definiscono gli assi cromatici: a* va dal verde al rosso, mentre b* va dal blu al giallo. In pratica, i valori di a* e b* si collocano solitamente tra -128 e +127, anche se possono superare leggermente questi limiti in base alle conversioni cromatiche.
| Colore 1 | Colore 2 | Valore di ฮE2000 |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
| Colore 1 | Colore 2 | Valore di ฮE2000 |
|---|---|---|
| 5 | ||
| 10 | ||
| 15 |
Parametri K_L, K_C e K_H
I parametri K_L, K_C e K_H sono fattori di ponderazione applicati ai termini di luminositร (ฮL*), croma (ฮC*) e tinta (ฮH*) nella formula CIEDE2000. Il loro valore predefinito รจ 1, che corrisponde alle condizioni di osservazione standard raccomandate dalla Commissione internazionale per lโilluminazione. In pratica, questi coefficienti vengono regolati per riflettere condizioni specifiche: ad esempio, K_L = 2 viene talvolta utilizzato per dare maggiore peso alle differenze di luminositร (comune nella stampa), mentre K_C o K_H possono essere ridotti per aumentare la tolleranza alle variazioni di saturazione o tinta a seconda delle esigenze del controllo qualitร . A seconda del contesto, questi coefficienti variano tipicamente tra 0,5 e 2.
ฮE2000 (CIEDE2000) misura la differenza percepita tra due colori: 0 significa colori identici, e valori piรน alti (fino a circa 185 nei casi estremi) indicano una differenza piรน evidente. Per esempio, un ฮE2000 intorno a 5 indica colori vicini, mentre intorno a 15 indica colori chiaramente distinti.
Esempio di utilizzo in Prolog
% Compute the Delta E (CIEDE2000) color difference between two L*a*b* colors in Prolog
color_1([69.5, 43.6, -1.8]).
color_2([70.2, 37.9, 1.6]).
extract_lab([L, A, B], L, A, B).
compute_delta_e :-
color_1(C1),
color_2(C2),
extract_lab(C1, L1, A1, B1),
extract_lab(C2, L2, A2, B2),
ciede_2000(L1, A1, B1, L2, A2, B2, DeltaE),
format('Delta E 2000 = ~10f~n', [DeltaE]).
% .................................................. This shows a ΔE2000 of 2.8044781137
% As explained in the comments, compliance with Gaurav Sharma would display 2.8044649638I risultati dei test
Il driver scritto in linguaggio C99, con 250 test statici precisi, ha dimostrato che questa funzione Prolog รจ interoperabile con la funzione CIEDE2000 disponibile in altri linguaggi di programmazione.
CIEDE2000 Verification Summary :
First Verified Line : 27,-123,101,44,-30,122,29.98937281745311
Duration : 254.09 s
Successes : 10000000
Errors : 0
Average Delta E : 63.2072
Average Deviation : 5.0e-15
Maximum Deviation : 1.1e-13File da scaricare
Sentitevi liberi di utilizzare questi file messi a disposizione da Michel, anche per scopi commerciali.
| File | Dimensione | Numero di clic |
|---|---|---|
| ciede-2000.pro | 5 KB | 80 |
| ciede-2000-driver.pro | 6 KB | 77 |
| test-pro.yml | 4 KB | 42 |
| reference-dataset.txt | 4 KB | 371 |
| Fai clic su pro.zip per scaricare tutti i file in un archivio. | ||
Comunitร
Cosa ne pensate di questo codice sorgente o di CIEDE2000? La vostra opinione รจ importante per noi! Il libro degli ospiti contiene giร 9 messaggi, di cui 1 in italiano. Date unโocchiata e condividete la vostra opinione.