简体   繁体   中英

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.

Using PIL, you can use the built-in histogram function. The "closeness" histograms can be calculated using any distance measure you want. For example, an L1 distance could be:

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:

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). 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 . If L2, then L2.

Your question hgas already been answered. Take a look at these other SO answers:

Algorithm for finding similar images

How can I quantify difference between two images?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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