Delta E 2000精度

2024-06-24 12:57:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用python实现了Delta CIE 2000算法。我不使用框架或外部库的原因是我以后需要将代码用于其他目的。我已经根据Wikipedia上描述的数学公式实现了该算法。它工作正常,但我发现输出值与colormath库中的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

下面是价值观的比较:

#LabColor (lab_l:60.2728 lab_a:29.1236 lab_b:12.3039) 
#LabColor (lab_l:62.6060 lab_a:40.7402 lab_b:-27.0242)
My code: 20.742092752610183
Colormath code: 21.159017320676828

其他实验室值也是如此。 作为健全性检查,我还使用了一个计算delta e cie 2000值(this is the website)的网站,它比我的更接近colormath值(21.159127)。我做错什么了?是小数精度的问题吗?如果是这样的话,我怎样才能使它更精确呢


Tags: labmathabssqrtfloatelifc2pcolor1