Image segmentation using k means clustering
이전 댓글 표시
Hello, I have a question and I appreciate your help. I don't know how to use a kmeans clustering results in image segmentation. I have an RGB image of a tissue which has 5 colors for 5 biomarkers and I need to do k means clustering to segment every color in a cluster. Thank you so much for your help.
채택된 답변
추가 답변 (1개)
Walter Roberson
2018년 4월 4일
0 개 추천
If you have 5 biomarkers then you would need to segment to a minimum of 6 clusters: one for each marker and one for tissue that is not one of the biomarkers. kmeans always assigns a cluster to every point, so if you had a point that was not one of the 5 colors and you asked to cluster it, then it would assign it to one of the five anyhow.
Note that the index values returned by kmeans are not in any pre-set order. You cannot assume that index 1 is associated with biomarker color #1. It is common for clusters to change effective identities during processing, so even if you had specified initial cluster centers in the options you passed to kmeans, you should not assume that the numbering of the output will be the same as the order of the initial cluster centers.
Remember too that a cluster centroid can be outside of data that belongs to that cluster. For example consider a perfect unfilled semi-circle of points: its centroid is the centre of the circle but no data points are at that center. You therefore cannot just index the centroid location into the image and look at the pixel value there to figure out what is happening.
댓글 수: 14
Penny13
2018년 4월 4일
편집: Walter Roberson
2018년 4월 4일
Walter Roberson
2018년 4월 4일
Which MATLAB release are you using?
Penny13
2018년 4월 4일
Walter Roberson
2018년 4월 5일
Could you post the entire error message ? And can you attach the .jpeg so we can test?
Penny13
2018년 4월 5일
Walter Roberson
2018년 4월 5일
No error for me here. Try
which -all kmeans
I suspect you are getting a third-party kmeans
Penny13
2018년 4월 5일
편집: Walter Roberson
2018년 4월 5일
Walter Roberson
2018년 4월 5일
I have not used that code; I do not work on image segmentation.
yuvraj singh
2018년 12월 20일
imsegkmeans doesn't work
Walter Roberson
2018년 12월 20일
imsegkmeans appears to have been introduced in R2018b in the Image Processing Toolbox. Are you using R2018b ?
Penny13
2018년 12월 21일
Image Analyst
2018년 12월 22일
Poupack, I use this code and I get no such error. Please try it. If you get an error, copy ALL the red text and paste it back here:
clear all;
close all;
he = imread('peppers.png');
% he = imread('11111.jpg');
% he = imread('tt.jpeg');
subplot(3, 3, 1);
imshow(he), title('H&E image');
cform = makecform('srgb2lab');
lab_he = applycform(he,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 5;
% repeat the clustering 5 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',5);
pixel_labels = reshape(cluster_idx,nrows,ncols);
subplot(3, 3, 2);
imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,5);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = he;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
subplot(3, 3, 3);
imshow(segmented_images{1}), title('objects in cluster 1');
subplot(3, 3, 4);
imshow(segmented_images{2}), title('objects in cluster 2');
subplot(3, 3, 5);
imshow(segmented_images{3}), title('objects in cluster 3');
subplot(3, 3, 6);
imshow(segmented_images{4}), title('objects in cluster 4');
subplot(3, 3, 7);
imshow(segmented_images{5}), title('objects in cluster 5');

SRUTHI BANDI
2019년 12월 22일
Error in demo1 (line 16)
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
error comes like this
Image Analyst
2019년 12월 22일
An error like what? You forgot to give the actual error message.
I just copied and pasted and ran it again successfully.
Are you sure you have the Stats toolbox?
카테고리
도움말 센터 및 File Exchange에서 Image Segmentation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!