Image palette reduction

Viewed 10109

I am playing with computer graphics programming for the first time. I want to convert RGB (24-bit) images to indexed-palette (8-bit) images (like GIF). My initial thought is to use k-means (with k=256).

How would one go about picking the optimal palette for a given image? This is a learning experience for me, so I would prefer an overview-type answer to source code.

Edit: Dithering is currently off-topic. I am only referring to "simple" color conversion, psycho-visual/perceptual models aside; color-space is also currently off-topic, though moving between color-spaces is what got me thinking about this in the first place :)

4 Answers

This might be a little late to answer, but try this:

  1. make a set of each color in the image,
  2. sort them according to red, then green then blue (if preceding channels are equal), (they are now into a list)
  3. suppress nearby colors if they are too similar, ie distance in rgb space is smaller than 4.
  4. if they are still to much colors, suppress the least used ones.

Each time you supress a color, you will have to add into a hash_map the colors and it's destination.

Related