Image segmentation using k means clustering
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Walter Roberson
il 4 Apr 2018
0 voti
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 Commenti
Penny13
il 4 Apr 2018
Modificato: Walter Roberson
il 4 Apr 2018
Walter Roberson
il 4 Apr 2018
Which MATLAB release are you using?
Penny13
il 4 Apr 2018
Walter Roberson
il 5 Apr 2018
Could you post the entire error message ? And can you attach the .jpeg so we can test?
Penny13
il 5 Apr 2018
Walter Roberson
il 5 Apr 2018
No error for me here. Try
which -all kmeans
I suspect you are getting a third-party kmeans
Penny13
il 5 Apr 2018
Modificato: Walter Roberson
il 5 Apr 2018
Walter Roberson
il 5 Apr 2018
I have not used that code; I do not work on image segmentation.
yuvraj singh
il 20 Dic 2018
imsegkmeans doesn't work
Walter Roberson
il 20 Dic 2018
imsegkmeans appears to have been introduced in R2018b in the Image Processing Toolbox. Are you using R2018b ?
Penny13
il 21 Dic 2018
Image Analyst
il 22 Dic 2018
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
il 22 Dic 2019
Error in demo1 (line 16)
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
error comes like this
Image Analyst
il 22 Dic 2019
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?
Categorie
Scopri di più su Image Segmentation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!