K-means Clustering for Image Segmentation to find sum of cluster's pixels

4 views (last 30 days)
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?

Accepted Answer

Pratyush Roy
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:
  1 Comment
Sebastian Bustillo
Sebastian Bustillo on 30 Oct 2020
Thank you so much for your help! I was wondering if I take the sum of the pixels from the original img matrix or from B (the output of overlay). Sorry if this is a dumb question but I'm very new to image segmenation with MATLAB.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!