Efficient multicolor search
An alternative approach might be to use the built-in ImageDistance function.
data = ResourceData["CIFAR-10", "TestData"][[;; ;; 100, 1]];
ColorSlider[Dynamic[targetColor]]
Dynamic[dist = ImageDistance[Image[targetColor], #] & /@ data;
Magnify@Row@data[[Ordering[dist, 5]]]]
Of course you can choose any of the possible DistanceFunctions. This can be sped up significantly by using a NearestFunction to calculate the ImageDistance between the target and the collection of images. For example:
data = ResourceData["CIFAR-10", "TestData"][[;; ;; 100, 1]];
nf = Nearest[data];
ColorSlider[Dynamic[targetColor]]
Dynamic[nf[Image[targetColor], 5]]
To address the issue of a multiple color selector, this can be incorporated smoothly by setting the "targetColor" image to have two different colors. Now the ImageDistance will react to both the chosen colors.
data = ResourceData["CIFAR-10", "TestData"][[;; ;; 100, 1]];
nf = Nearest[data];
{ColorSlider[Dynamic[targetColor1]], ColorSlider[Dynamic[targetColor2]]}
Dynamic[targetColorAll =
ImageAssemble[{Image[targetColor1], Image[targetColor2]}]]
Dynamic[nf[Image[targetColorAll], 5]]
Here's a NearestFunction
that will do what you did with Table
:
nf = Nearest[
Hold /@ cols -> "Index", (* Hold seems to be necessary to make sure Nearest doesn't get confused by the inner lists *)
DistanceFunction -> Function[
Min @ Divide[
Map[
Function[col,
ColorDistance[#1, col]
],
#2[[1, All, 1]]
],
#2[[1, All, 2]]
]
]
];
I also recommend that you use the TrackedSymbols -> {targetColor}
option in your Dynamic
to make sure it only updates whenever targetColor
changes.