python - 将基本颜色列表与另一个颜色列表进行比较
问题描述
我有 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
解决方案
我认为您计算分数的部分不正确。您在全球范围内计算 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。
推荐阅读
- python - BeautifulSoup4 抓取 URL 不变的页面 [Python]
- javascript - 这个想法是点击标签以显示下拉菜单项
- flutter - 在 Flutter 中更改选项卡时添加进度指示器
- excel - 检查时间是否在vba中的特定范围内
- c# - Xamarin Essentials 连接始终返回蜂窝连接
- hl7-fhir - 暴露给 FHIR 服务器的电子健康记录
- c - 引发 FE_INEXACT 后执行的典型用户定义逻辑是什么?
- sql - 映射到 SQL 中许多顺序子案例的第一个父案例
- reactjs - 有没有办法将 ID 传递给 modal 以呈现每个用户的正确上下文?
- python - 根据条件颜色绘制图例