
Subhabrata Bhattacharya CAP 5415 HW PS03
1. We are required to learn the K-means clustering algorithm through this exercise. In this problem we create inputs
for the kmeans-clustering algorithm. Each pixel in an image has certain features according to which it could be
clustered with others,leading to a segment. We select the pixel’s RGB values and the xy co-ordinates to be its set
of distinguishing feature and create a 5-dimensional feature vector from it. The following matlab code extracts the
required features from an image matrix and generates an n X 5 feature matrix, where n is the number of pixels in
a given image.
1% This function takes a 3D matrix representing the RGB color map and the
2% pixel's spatial co−ordinates. Returns a feature matrix by constructing a
3% 5dimensional feature vector for each pixel.
4
5function featmat = im2featurevec(img)
6[nrow ncol cmap] = size(img);
7for icnt = 1:cmap
8% Separately gather the RGB components
9colcomp = img(:,:,icnt);
10 colcomp = colcomp(:);
11 fvect(:, icnt) = colcomp;
12 end
13 rowcnt =1;
14 colcnt =1;
15 for icnt = 1:nrow*ncol
16 if(mod(icnt,nrow) ==0)
17 rowcnt = 1;
18 colcnt = colcnt + 1;
19 end
20 fvect(icnt,4) = rowcnt;
21 fvect(icnt,5) = colcnt;
22 rowcnt = rowcnt + 1;
23 end
24 featmat = fvect;
25 return
Listing 1: Matlab function to extract features from an image matrix and generate a feature matrix from the extracted
features
2. Next we implement the k-means clustering algorithm which takes the feature matrix from the above step as input.
The algorithm is also specified with the number of clusters that are to be used to put the pixels into. Initially
through random selection we select the cluster centers from all the candidate pixels. After every iterations, the
cluster centers are modfied to reflect the more ’appropriate’ centers of the pixel distribution. The pixels that are
closest to the newly modified centers, are assigned to that center and this goes on until the algorithm converges.
We restrict the maximum number of iterations the algorithm performs to converge as 5. The following matlab
function describes the k-means clustering algorithm.
1% This function implements the K Means clustering algorithm.It takes pts as
2% input which is a 5 dimensional feature vector. It also takes Nc as the
3% number of clsuter centers to which each points are going to be assgined.
4% Another argument Ni specifies the number of iterations, it is selected by
5% the user.
6function assign = getKMeansCluster(pts, Nc, Ni)
7[nvect nfeat]= size(pts);
8% Randomly select cluster centers, this is initial estimation, thus
9% initassign has randomly allocated Nc indices of the feature vector array
10 initassign = uint32(nvect*rand(1,Nc));
11 % To ensure that the cluster centroids are unique
12 while(1)
13 initassign = uint32(nvect*rand(1,Nc));
14 nclus = unique(initassign);
15 if Nc == length(nclus)
16 break;
17 end
18 end
19 assign = ones(nvect,1);
20 citer = 1;
21
22 % Now we would assign the points to the cluster centroids found from the
23 % above step.
1