使用Python查找具有类似调色板的图像

时间:2009-11-10 00:03:40

标签: python image colors

假设图库中有10,000个JPEG,PNG图像,如何找到所有具有相似调色板的图像到所选图像,并按降序相似度排序?

2 个答案:

答案 0 :(得分:10)

为每个图像构建颜色直方图。然后,当您想要将图像与集合匹配时,只需按照直方图与所选图像的直方图的接近程度对列表进行排序。

存储桶的数量取决于您希望的准确度。组合成一个存储桶的数据类型将定义您优先搜索的方式。

例如,如果您对色调最感兴趣,那么您可以定义图像的每个像素所在的桶:

def bucket_from_pixel(r, g, b):
    hue = hue_from_rgb(r, g, b) # [0, 360)
    return (hue * NUM_BUCKETS) / 360

如果您还想要一般的匹配器,那么您可以根据完整的RGB值选择桶。

使用PIL,您可以使用内置的histogram功能。可以使用您想要的任何距离测量来计算“接近度”直方图。例如,L1距离可以是:

hist_sel = normalize(sel.histogram())
hist = normalize(o.histogram()) # These normalized histograms should be stored

dist = sum([abs(x) for x in (hist_sel - hist)])

L2将是:

dist = sqrt(sum([x*x for x in (hist_sel - hist)]))

Normalize只是强制直方图的总和等于某个常数值(1.0工作正常)。这很重要,因此可以将大图像与小图像进行正确比较。如果您要使用L1距离,则应在normalize中使用L1度量。如果是L2,那么L2。

答案 1 :(得分:0)

您的问题已经得到解答。看看其他SO答案:

Algorithm for finding similar images

How can I quantify difference between two images?