Having an image I want to reduce it to x
colors and save it in a png file.
I choose to do this using k-means (which is already available in OpenCV). On a full resolution image it takes more then a minute so I resize the image to something much smaller and I get the color palette.
What would be the best way to replace the colors in the original image with the ones in the palette?
One simple implementation I did was to take each pixel in the image and calculate the euclidian distance between that pixel the one in the palette, and then choose the color in the palette that is closest. The visual aspect of this is ok, but it takes more than 30 seconds for palette size grater than 30.
I understand that the way to go is to use Octree or some form of k-nearest neighbors algorithm, but to be honest I don't know if I should spent the time to implement something that I think it's already implemented.
Can I use something from OpenCV that suits my scenario? And can I expect that it will it be at least 2 times faster than using the euclidian distance?
C++ examples would be appreciated.