简体   繁体   English

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

[英]Find images with similar color palette with Python

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

Build a color histogram for each image. 为每个图像构建颜色直方图。 Then when you want to match an image to the collection, simply order the list by how close their histogram is to your selected image's histogram. 然后,当您想要将图像与集合匹配时,只需按照直方图与所选图像的直方图的接近程度对列表进行排序。

The number of buckets will depend on how accurate you want to be. 桶的数量取决于您想要的准确度。 The type of data combined to make a bucket will define how you prioritize your search. 组合成一个存储桶的数据类型将定义您如何确定搜索的优先级。

For example, if you are most interested in hue, then you can define which bucket your each individual pixel of the image goes into as: 例如,如果您对色调最感兴趣,那么您可以定义图像的每个像素所在的桶:

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

If you also want a general matcher, then you can pick the bucket based upon the full RGB value. 如果您还需要通用匹配器,则可以根据完整的RGB值选择存储桶。

Using PIL, you can use the built-in histogram function. 使用PIL,您可以使用内置histogram功能。 The "closeness" histograms can be calculated using any distance measure you want. 可以使用您想要的任何距离测量来计算“接近度”直方图。 For example, an L1 distance could be: 例如,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)])

an L2 would be: L2将是:

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

Normalize just forces the sum of the histogram to equal some constant value (1.0 works fine). Normalize只是强制直方图的总和等于某个常数值(1.0工作正常)。 This is important so that large images can be correctly compared to small images. 这很重要,因此可以将大图像与小图像进行正确比较。 If you're going to use L1 distances, then you should use an L1 measure in normalize . 如果您要使用L1距离,那么您应该在normalize使用L1度量。 If L2, then L2. 如果L2,那么L2。

Your question hgas already been answered. 你的问题已经得到了解答。 Take a look at these other SO answers: 看看这些其他SO答案:

Algorithm for finding similar images 寻找相似图像的算法

How can I quantify difference between two images? 如何量化两幅图像之间的差异?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM