I'm hoping someone can help me improve the following fun code-snippet, which takes a target color and searches through a set of images to find the ones that most closely match:
data = ResourceData["CIFAR-10", "TestData"][[;; ;; 100, 1]];
cols = DominantColors[#, 10, {"Color", "Coverage"}] & /@ data;
ColorSlider[Dynamic[targetColor]]
Dynamic[distances = Table[Min[(Function[c, ColorDistance[c, targetColor]] /@ cc[[All, 1]])*(1/cc[[All, 2]])], {cc, cols}];
Magnify@Row@data[[Ordering[distances, 5]]]
]
Ok, so it basically works. But here are the two things missing:
Problem 1. I’m hoping someone can help make it more efficient so it can scale a little better, the example runs on only 100 images. I would like to replace the brute-force Table
with a call to Nearest
- but I'm not quite sure how.
Problem 2. I'd also like to search based on multiple colors (2 or more weighted colors), but have not had much success. Here's the naive approach of adding the distances:
Column@{ColorSlider[Dynamic[targetColor1]], ColorSlider[Dynamic[targetColor2]]}
Dynamic[distances = Table[Min[(Function[c, ColorDistance[c, targetColor1]] /@ cc[[All, 1]])*(1/cc[[All, 2]]) + (Function[c, ColorDistance[c, targetColor2]] /@ cc[[All, 1]])*(1/cc[[All, 2]])], {cc, cols}]; Magnify@Row@data[[Ordering[distances, 10]]]]
This doesn't work very well, I'm thinking of perhaps tweaking the distance function and using the LAB colorspace.
References & Links:
Helpful examples and related articles: