I have a problem with deciding algorithm for color quantization. The image that I want to do color quantization is an RGB image with resolution 512 x 512. I want to reduce the color value in the pixel to reduce the image size.

I don't want to use the popular algorithm k-means. And I found this mean shift algorithm.

Mean shift is a clustering algorithm (same as k-means) that have certain centroids and window (each centroids have 1) to look for densest part in its window then move the centroid into the densest part. It will keep going like that until it converge with another centroid and not moving anymore.

Is it possible to use mean shift in color quantization? Because both k-means and mean shift are clustering algorithms but all I found is image segmentation for mean algorithm and k-means for color quantization?

  • @DocBrown I add some details about the problems but maybe is still can be confusing Jan 28, 2019 at 7:00

1 Answer 1


Sure it can.

I am pretty sure I did not understand mean shift in full. But it is quite clear it is a clustering algorithm (well, I think it is more a class of algorithms, nothing which you can just implement without making further decisions about some algorithmic details). But let us assume you got an implementation, then apply the algorithm in some appropriate color space for a given image. Afterwards calculate the center of each cluster. Then replace the color of each point by the center value of its cluster.

Opposed to k-means, one does not have to choose the number of colors for the clustering beforehand (instead, you have to choose a "kernel function" and/or "window size"). If that is really an advantage about k-means for your specific use case is something you have to decide for yourself.

Disclaimer: I have implemented an k-means color quantization algorithm some time ago by myself, took me some effort to get all the details right.

  • actually it's kind of an experiment using another clustering alogrithm, but I think i'm gonna stay with mean shift. But to summarize what you are saying, I'm suppose to think further about some algorithmic details right? Thanks for your answer really. Feb 1, 2019 at 11:37
  • 1
    @user10057710: you asked a "yes or no" question. I think the answer is "yes, it could work", based on the little I know about those color quantization algorithms. But the next step is to try "mean shift" out, get all the gory details right, and check if the results are useful for your use case, which is nothing I can really help you with.
    – Doc Brown
    Feb 1, 2019 at 11:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.