4 – K-means Clustering

One commonly used image segmentation technique is k-means clustering. It’s a machine learning technique that separates an image into segments by clustering or grouping together data points that have similar traits. As an example, let’s look at this image of oranges in a bowl. If I asked k-means to break up this image into two different colors, it will give us an image that looks like this. The oranges and the lighter part of the table are all considered to be one cluster of orange points. And the dark background and any part in shadow are clustered as dark brown data points. But, we’re getting ahead of ourselves, let’s talk about how k-means clusters this data step by step. K-means is called an unsupervised learning method which means you don’t need to label data. Instead, unsupervised learning aims to group and characterize unlabeled datasets. And, in doing so, often identifies patterns and similarities in groups of data. So, you can give k-means a set of any unlabeled data, like the pixel values in an image, and just tell it to break it into k clusters where k is a variable whose value you choose. For the orange example, we chose a k = 2. And if we increased that to say, k equals three or six, we would get three or six different colored segments. In this case, those segments divide differently shaded parts of the oranges and the table. Let’s go through a simple example in even more detail. Here’s a small image just 34 by 34 pixels of a rainbow pattern. I’ll use k-means to separate this image into three clusters based on color. To start, we know that each pixel in this image has an associated RGB value. In fact, we can actually plot the value of each pixel as a data point in RGB color space. The axis are just values for R, G, and B. And notice the values on this axis. Here, the highest R, G, and B values say, close to white will be at this top corner. And if we turn this space, we can actually see that the points fall into natural color clusters. If I tell k-means to separate this image data into three clusters, it will look at these pixel values and randomly guess three RGB points that partition the data into three clusters. Let’s call these points center point A, center point B, and center point C. It then assigns every pixel value to a cluster based on its closest center point. So all of these pixels on the left belong to cluster A. And these more to the right to cluster B. And these on the bottom, to cluster C. And here, I divided each into a separately colored region. k-means then takes the actual average or mean of all the RGB values in each cluster. And these mean values become the updated values for each center point. Let’s go back a step and take one cluster as an example, Cluster A, k-means looks at all the pixel values in this cluster and computes a mean RGB value. Let’s call that mean A. It then updates the position of the center point A to be this mean value. This is why I initially called these center points. They’re supposed to be in the center of their cluster. And it does the same thing for cluster B and center point B and cluster C and center point C, moving the guessed center points to the mean of their cluster values. Then, this process repeats. New clusters are formed based on where they are in relation to these new adjusted center points. And then, an updated mean is calculated from these clusters and the center points are updated again. So, there’s a sort of pulling done by the data points to create new center points. This process repeats again and the center points will generally move a smaller and smaller distance from their previous value after each iteration. The algorithm keeps repeating these steps until it converges and the convergence is defined by us. It’s usually after a number of iterations, say 10, or based on how much the center points move after each iteration. If the center points move less than some small value, say, one pixel during an iteration, then the center points have converged and the algorithm is finished. The center points that arise at the end of this process should best separate the data. Next, let’s see how to do this clustering in code.

Dr. Serendipity에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

Continue reading