Finding the dominant colors in an image with k-means

Working with images can be a very time-consuming task, especially if you have many images to work on. Machine learning can thus be a great time-saver for various image analysis and editing tasks, such as finding the dominant colors of an image thanks to the K-means clustering algorithm.

The K-means clustering algorithm defines a number K of clusters and the best “centroids” to cluster the data around. When applied to images, it allows extracting the k dominant colors in an image to be used for other purposes.

Thankfully, all the programming has already been done by Shivam K Thakkar, you just need to follow his post and everything works like a charm, allowing to extract as few or as many dominant colors from any image. The Python code relies on OpenCV, Scikit-Learn, Numpy, and Matplotlib for visualization, which must, therefore, be installed first.

Graph representing pixels by dominant colors obtained with K-means

However, Shivam’s code is built to extract colors from a small image and tends to be slow for larger images. If you are interested in using it for larger images, just use the code below which first reduces the size of images, vastly reducing the computing time.

Just modify the scale_percent = 10 parameter to make sure the size of the reduced images is large enough to extract proper colors and small enough to remain in reasonable computing time.

2 Replies to “Finding the dominant colors in an image with k-means”

  1. Hi,
    Thanks for your post.
    I am just wondering why have you taken clusters = 5. why just not 2 or 6.
    what are the criteria need to taken by considering n_clusters?


    1. Hi,
      Well, I needed to have 5 colors extracted from images for this actual project. But you can totally take a different number of clusters depending on your needs.
      Unlike the K-Nearest Neighbors algorithm, the value of K for K-means doesn’t need to be odd, it can as easily be even.

Leave a Reply

Your email address will not be published. Required fields are marked *