Image Segmentation With K-Means Clustering

An introduction with Python

You can view the notebook for this project here

LEFT: Original Photograph, RIGHT: Segmented image (5 colours/segments)

Looking at the images above, we see an example of an image posterization filter that gives images a cartoon-like appearance, but behind the scenes, this filter is actually using a machine learning algorithm known as clustering.

Before exploring into how this process works and seeing how we can implement it in Python, let’s take a look at why we might want to do this in the first place.

Image Segmentation

In a normal photograph, a pixel can take one of roughly 16.7 million different colours. In this processed image, however, there are only 5 different colours. We’ve split all of the pixels into 5 different groups, segmenting the image into these different colour-regions.

We’ve also cut down on the amount of noise and variation within the image. So, if this were to be used in some other machine learning application, we’ve just massively reduced the amount of data that needs to be processed, especially if this were applied to a whole library of images.

Even though we’ve simplified this image, we’ve still retained most of the important structural data. We are still able to identify shapes and forms, shadows and…