Downsample the image first, then run k-means.
If you resize the image to 1/2th in both x and y, it shouldn't affect colors much, but k-means should take at most 1/4th of the time. If you resample to 1/10 of the width and height, k-means should run 100 times faster.
https://en.wikipedia.org/wiki/Color_quantization
By downsampling the image, you have less "pixels" to process during clustering. But in the end, it should produce roughly the same color scheme.
Small summary of k-means:
- It maps each object (=pixel) to the nearest cluster center (= palette entry)
- It recomputes each palette entry to best represent the assigned points (= pixels)
- Repeat until nothing changes anymore.
So the real output is not an image or image regions. It's the palette.
You can then map an arbitrary image (including the full resolution version) to this color palette by simply replacing each pixel with the closest color!
Complexity and performance:
The complexity of k-means is O(n*k*i)
, where n
is the number of pixels you have, k the desired number of output colors and i the number of iterations needed until convergence.
n
: by downsampling, you can easily reduce n
, the largest factor. In many situations, you can reduce this quite significantly before you see a degradation in performance.
k
: this is your desired number of output colors. Whether you can reduce this or not depends on your actual use case.
i
: various factors can have an effect on convergence (including both other factors!), but the strongest probably is having good starting values. So if you have a very fast but low quality method to choose the palette, run it first, then use k-means to refine this palette. Maybe OpenCV already includes an appropriate heuristic for this though!
You can see, the easiest approach is to reduce n
. You can reduce n
significantly, produce an optimized palette for the thumbnail, then rerun k-means on the full image refinining this palette. As - hopefully - this will reduce the number of iterations significantly, this can sometimes perform very well.