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

조회 수: 1 (최근 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개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by