# K-means clustering to generate probability maps

17 views (last 30 days)
Nathaniel Bennett on 28 Jan 2022
Answered: Prachi Kulkarni on 16 Feb 2022
hello,
I am trying to use k-means in an iterative fashion to generate cluster maps based on randomly sampled pixels from an image. being in color or grayscale does not matter for this assignment. i have some code, but it generates a bar style graph instead of a cluster map. im also attaching the assignment details so you might have a better understanding for what i am trying to do.
1. implement "iterative" k-means, as described below, and leave k (number of clusters) as an open parameter to be set by the user
2. cluster the image pixels with respect to color, and display the corresponding probability maps
Iterative k-means:
Every execution of k-means is an iterative process, from start to convergence, using randomly chosen cluster centroids during initialization. Consider an execution as a single instance in a "bigger" iterative process where you will execute k-means with different randomly chosen centroids. The output should be probability maps: e.g., for k=3, output should be three maps:
P(x belongs to k = i) | F ), for i={1,2,3} (F = how often a pixel "jumps" to a different cluster, or how salient the assignment of x to i is).
Example:
step 1: load image into variable X (of size h x w x 3)
step 1.5: numofexec = 1 (counts how many times you execute k-means; initialize to 1)
step 2: randomly choose three pixels and use their color as initialization in the color space; i.e., {C1, C2, C3} for k = 3, where C1, C2, C3 are 3x1 (or 1x3) matrices.
step 3: perform k-means: Xout = somekmeansfun(X, k, C1, C2, C3) ==> notice the input: variable k, and random centroids ==> let the user define k prior to step 1. The output Xout should be of size h x w x 1. For more compact representation (see below), M( : , : , numofexec) = Xout. See notes below.
step 4: numofexec = numofexec + 1; Repeat step 2 ; repeat step 3.
step 5: At the end, you will have a 3-dimensional matrix M: size(M) = [h, w, numofexec]. You can visualize the cluster assignments of the pixel X(i, j) as: v = M(i, j, :) ==> size(v) = [1, numofexec] (1 x numofexec) ==> plot it. For each pixel, you can calculate the frequency of its assignment to each cluster using v.
Notes:
-- steps 2-4 should be in a for-loop.
-- in each exec., the cluster numbers may change: you need to reassign numbers based on the centroid values (e.g., use as reference the value of a chromatic component). For example: the "deep red" (high R-values, very low G,B-values) cluster may be labeled as "1" in one k-means execution, and as "2" in another execution; you will have to align/readjust those cluster number after each k-means execution.
-- to randomly choose centroid values, you can use a random integer generator that will be sampling the image pixels.
the code i currently have is this: ( it has been cobbled together from questions and answers on this site.)
clc;
%3k = input('Enter the Number of clusters: ');
[height, width, dimensions] = size(J);
points = double(reshape(J, height * width, dimensions));
%Z = im2gray (J);
POINT1 = J(:, :, 1);
POINT2 = J(:, :, 2);
POINT3 = J(:, :, 3);
locations1 = randperm(numel(POINT1),3);
locations2 = randperm(numel(POINT1),3);
locations3 = randperm(numel(POINT1),3);
C1 = (points(locations1))
C2 = (points(locations2))
C3 = (points(locations3))
numofexec = 1;
[L,Centers] = imsegkmeans(J,k);
[idx,C] = kmeans(points,3, 'Start', [C1;C2;C3]);
subplot(2, 1, 1);
gscatter(points(:,1),points(:,2),idx,'bgm')
hold on
plot(C(:,1),C(:,2),'kx')
legend('Cluster 1','Cluster 2','Cluster 3','Cluster Centroid')
C

Prachi Kulkarni on 16 Feb 2022
Hi,
You can create the M matrix as specified in the problem with the following starter code. You may have to manually reassign numbers based on the centroid values as required in the question.
clc
k = input('Enter the Number of Clusters: ');
[height, width, dimensions] = size(J);
points = reshape(im2double(J), height * width, dimensions);
totalnumofexec = 5;
M = zeros(height,width,totalnumofexec);
for i=1:totalnumofexec
locations = randperm(size(points,1),k);
C = points(locations,:);
idx = kmeans(points,k,Start=C);
M(:,:,i) = reshape(idx,height,width);
figure, imagesc(M(:,:,i));
end

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!