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

4 次查看(过去 30 天)
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?

采纳的回答

Pratyush Roy
Pratyush Roy 2020-10-27
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 个评论
Sebastian Bustillo
Sebastian Bustillo 2020-10-30
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.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by