我已经在python中实现了Delta CIE 2000算法。我之所以不使用框架或外部库,是因为以后需要将我的代码用于其他目的。我已经根据Wikipedia中描述的数学公式实现了该算法。效果很好,但我发现输出值与colormath库中cls
实现的输出不匹配。
首先,这是我的代码:
delta_e_cie_2000()
这是值的比较:
# Input colors should be in lab
def DE2000(color1, color2):
dLp = float(color2[0] - color1[0])
L_ = float((color1[0] + color2[0]) * 0.5)
C1 =float( math.sqrt(color1[1] ** 2 + color1[2] ** 2))
C2 =float( math.sqrt(color2[1] ** 2 + color2[2] ** 2))
C_ = (C1 + C2) * 0.5
a1p = color1[1] + (color1[1] * 0.5) * (1 - math.sqrt((C_ ** 7) / (C_ ** 7 + 25 ** 7)))
a2p = color2[1] + (color2[1] * 0.5) * (1 - math.sqrt((C_ ** 7) / (C_ ** 7 + 25 ** 7)))
C1p = math.sqrt(a1p ** 2 + color1[2] ** 2)
C2p = math.sqrt(a2p ** 2 + color2[2] ** 2)
Cbarp = (C1p + C2p) * 0.5
dCp = C2p - C1p
h1p = math.atan2(color1[2], a1p) % 360
h2p = math.atan2(color2[2], a2p) % 360
print("h1p:", h1p)
print("h2p:", h2p)
print("abs(h1p - h2p):", abs(h1p - h2p))
if abs(h1p - h2p) <= 180:
dhp = h2p - h1p
elif abs(h1p - h2p) > 180 and h2p <= h1p:
dhp = h2p - h1p + 360
elif abs(h1p - h2p) > 180 and h2p > h1p:
dhp = h2p - h1p - 360
else:
dhp = 0
dHp = 2 * math.sqrt(C1p * C2p) * math.sin(dhp * 0.5)
if abs(h1p - h2p) <= 180:
Hbarp = (h1p + h2p) * 0.5
elif abs(h1p - h2p) > 180 and h1p + h2p < 360:
Hbarp = (h1p + h2p + 360) * 0.5
elif abs(h1p - h2p) > 180 and h1p + h2p >= 360:
Hbarp = (h1p + h2p - 360) * 0.5
elif C1p == 0 or C2p == 0:
Hbarp = (h1p + h2p)
T = 1 - (0.17 * math.cos(Hbarp - 30) +
0.24 * math.cos(2 * Hbarp) +
0.32 * math.cos(3 * Hbarp + 6) -
0.20 * math.cos(4 * Hbarp - 63))
SL = 1 + (
(0.015 * (L_ - 50) ** 2)
/ (math.sqrt(20 + ((L_ - 50) ** 2)))
)
SC = 1 + 0.045 * Cbarp
SH = 1 + 0.015 * Cbarp * T
Rt = -2 * math.sqrt((Cbarp ** 7) / (Cbarp ** 7 + 25 ** 7)) * math.sin(60 * math.exp(-((Hbarp - 275) / 25) ** 2))
kl = 1
kc = 1
kh = 1
dE2000 = math.sqrt(
(dLp / (kl * SL)) ** 2
+ (dCp / (kc * SC)) ** 2
+ (dHp / (kh * SH)) ** 2
+ Rt * (dCp / (kc * SC)) * (dHp / (kh * SH))
)
return dE2000
其他实验室值也是如此。 作为健全性检查,我还使用了一个网站来计算增量cie 2000值(this is the website),它比我的更接近色度值(21.159127)。我究竟做错了什么?这是十进制精度的问题吗?如果是这样,我如何使其更精确?