K-means Clustering for Image Segmentation to find sum of cluster's pixels
1 view (last 30 days)
Show older comments
Sebastian Bustillo
on 24 Oct 2020
Commented: Sebastian Bustillo
on 30 Oct 2020
Hello MATLAB community,
I've been using K-mean clustering for image segmentation; this is electron beam data. I've used to code below:
img = data.img(100:500,400:900); % Select the relevant part of the image
[L,Centers] = imsegkmeans(img,8); % Perform image segmentation with 8 centroids
B = labeloverlay(img,L);
imshow(B); % Display it
This code produces the following output:

There are 8 "islands" that represent a quantity that needs to be calculated. What I need to do is to calculated the pixels that account for these "islands". Any suggestions how I may be able to do this?
0 Comments
Accepted Answer
Pratyush Roy
on 27 Oct 2020
Hi Sebastian,
Assuming that the goal is finding the pixels corresponding to a particular cluster, one can use the "find" function to get the indices of the pixels corresponding to a particular cluster.
Here's a code snippet that can be used to get the pixel indices
[R1,C1] = find(L,1);% R1 gives us the row indices and C1 gives us the column indices for the points with label 1.
[R2,C2] = find(L,2);% Similarly we can get for pixels for label 2
In this way we can get the indices for the pixels for all the 8 clusters. To get the pixel intensity values,one can append all the intensity values to an empty array "arr" to get the intensity values of the pixels belonging to the same cluster.
arr = [];% Stores pixel intensity values for cluster 1. One can have a different empty array for a different cluster.
for i=1:length(R1)
J = I(R1(i),C1(i));
arr = [arr J];
end
You can go through the following documentation link for further help:
More Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!