首页 > 解决方案 > 将基本颜色列表与另一个颜色列表进行比较

问题描述

我有 2 个颜色数组/列表。一个代表图像中的实际主色,另一个代表算法认为是图像中主色的列表颜色。

我想比较这 2 个列表,以了解该算法与实际的真实颜色列表的接近程度。*如果它们之间的欧几里得距离 <= 25,则认为 2 种颜色很接近(相同)。

问题是 2 个列表的长度不一定相同。每个列表中的颜色不会按任何顺序排列。颜色将始终位于颜色空间CieLAB( cv2.COLOR_BGR2LAB) 中。

我需要比较相似性的 2 个列表的示例。请注意,它们的顺序和列表长度不同。但是这两个被认为是相同的,因为它发现了地面实况中的所有颜色(加上一些额外的颜色)并且所有这些颜色都 <= 25 距离。

ground_truth = [[76, 177, 34], [36, 28, 237], [204, 72, 63], [0, 242, 255]]
结果 = [[35, 29, 234], [200, 72 , 63], [70, 177, 34], [0, 242, 250], [45,29,67], [3,90,52]]

我在下面建立了一个验证器,但我不确定它是否正确?关于如何实现上述目标的任何建议?

def validator(algoTuner, result, ground_truth):
    # Score = How close the colours in result are to the colours in ground_truth

    # p = pairwise distance between result and ground_truth
    # hits = get all distances from p that are <= max_dist
    # score = float(len(hits)) / len(ground_truth)
    dists = cdist(result, ground_truth, 'euclidean')
    score = float(len(dists[ dists < 25 ])) / len(ground_truth)
    return score

在@Nakor 的回答之后编辑,这会更正确吗?请记住,该算法可以找到比基本事实更多的颜色。重要的是算法会在地面实况中找到所有正确的颜色,任何额外的颜色都不会影响分数。

def validator(algoTuner, result, ground_truth, debug=False):
    dists = cdist(result, ground_truth, 'euclidean')
    correct_guesses = np.sum(dists<25, axis=1)
    correct_guesses = correct_guesses[ correct_guesses > 0 ]
    # score = correct_guesses.mean()
    score = float(correct_guesses.size) / len(ground_truth)

    if debug:
        print(len(correct_guesses))
        print(correct_guesses)
        print(score)
    return score

标签: pythonnumpyopencvscipy

解决方案


我认为您计算分数的部分不正确。您在全球范围内计算 25 以下的元素数量。但是,如果我理解正确,您正在寻找,对于其中的每种颜色,ground_truth是否至少有一种颜色result距离小于 25 点。

如果是这种情况,那么我将修改您的验证器:

def validator(algoTuner, result, ground_truth):
    # Score = How close the colours in result are to the colours in ground_truth

    # p = pairwise distance between result and ground_truth
    # hits = get all distances from p that are <= max_dist
    # score = float(len(hits)) / len(ground_truth)
    dists = cdist(result, ground_truth, 'euclidean')
    correct_guesses = np.sum(dists<25,axis=0)
    score = (correct_guesses>0).mean()
    return score

它返回结果中也存在的 ground_truth 中颜色的比例。在您的示例中,分数将为 1。


推荐阅读