Suppose there are 10,000 JPEG, PNG images in a gallery, how to find all images with similar color palettes to a selected image sorted by descending similarity?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With