

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A problem set for a university course, cmsc 426, which involves implementing the k means algorithm to cluster pixels in an image based on their intensity. The assignment includes instructions for writing functions to assign pixels to clusters, display the results, and compute cluster centers. The problem set also includes a challenge to extend the k means program to work on color images and an extra challenge to implement em for color images.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


The goal of this assignment is for you to implement the K Means algorithm to cluster pixels in an image by their intensity. This algorithm works iteratively. We are given an image and a number k, which indicates the number of clusters we are to produce. First, we guess some reasonable values for the central intensity of each cluster. Then we alternate between performing the following two steps. 1) Every pixel is assigned to a cluster. A pixel is assigned to the cluster whose central intensity is closest to the pixel’s own intensity. 2) For every cluster, we recompute the central intensity as the mean intensity of all pixels that have been assigned to that cluster. We stop this iteration when the centers of the cluster stop changing.
Here’s an example of how this might work. Suppose the “image” consists of three pixels with intensities: 0, 100, 255, and we want to form these into two clusters. We might start out by guessing random intensities between 0 and 255 for the two cluster centers. For example, we might guess the values 30 and 140. Then we would assign the pixel with intensity 0 to the cluster centered at 30, and we would assign pixels with intensities 100 and 255 to the cluster centered at 140. Next, we would assign the first cluster a center of 0, and the second cluster a center of 177.5. In the next iteration, none of the cluster assignments would change, so none of the cluster centers would change, and we would stop.
Turn in your documented code, and a run showing the output of your code on the example given in the previous paragraph.
Test this function using the swanbw.jpg image. Use your function in problem 1 to assign each pixel to one of the centers 90, 150 or 230. Then use these assignments to generate an image in which each pixel is either 90, 150 or 230. Display this image using imshow (keep in mind, that to use imshow, your image should be converted back to type uint8. There is a function called uint8 that will convert a matrix to this type). Turn in your documented code, and the image you generate. The image our code produces is shown in swanbw_KMeans_90_150_230.jpg.
Turn in your documented code, and a run showing the output of your code on the example given in the previous paragraph.
Now run your code on the swan image, forming 4 clusters. Display the resulting image in a figure, and turn this in. Run your code five times, and comment on whether you notice any differences in the output on different runs.