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.

 採用された回答

Image Analyst
Image Analyst 2018 年 12 月 20 日
編集済み: Image Analyst 2018 年 12 月 20 日

1 投票

I'm attaching a color kmeans demo that doesn't require a recent version of the stats toolbox or image processing toolbox.
Personally I don't think kmeans works that great (perhaps because it's untrained), as you'll probably see, and you might try a trained discriminant analysis instead. I attach demos for both.

1 件のコメント

Penny13
Penny13 2018 年 12 月 21 日
Thank you so much Image Analyst. I will try both to see what would be the results.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
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
Penny13 2018 年 4 月 4 日
編集済み: Walter Roberson 2018 年 4 月 4 日
Thank you so much for your answer.
clear all;
close all;
he = imread('48rf.jpg');
% he = imread('11111.jpg');
% he = imread('tt.jpeg');
figure;
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);
figure;
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
figure;
imshow(segmented_images{1}), title('objects in cluster 1');
figure;
imshow(segmented_images{2}), title('objects in cluster 2');
figure;
imshow(segmented_images{3}), title('objects in cluster 3');
figure;
imshow(segmented_images{4}), title('objects in cluster 4');
figure;
imshow(segmented_images{5}), title('objects in cluster 5');
Sir, I used this code from one MATLAB blogs on my image, but it keeps telling me that too many input arguments. Could you please help me what is the problem? My apologies if my question is basic.
Walter Roberson
Walter Roberson 2018 年 4 月 4 日
Which MATLAB release are you using?
Penny13
Penny13 2018 年 4 月 4 日
MATLAB_R2017b
Walter Roberson
Walter Roberson 2018 年 4 月 5 日
Could you post the entire error message ? And can you attach the .jpeg so we can test?
Penny13
Penny13 2018 年 4 月 5 日
Error using kmeans Thanks so much. I attached the image.
Too many input arguments. Error in C_B_Seg_K_Means_Clustering (line 19) [cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
Walter Roberson
Walter Roberson 2018 年 4 月 5 日
No error for me here. Try
which -all kmeans
I suspect you are getting a third-party kmeans
Penny13
Penny13 2018 年 4 月 5 日
編集済み: Walter Roberson 2018 年 4 月 5 日
Thank you so much for your reply. I used the code from this webpage: https://www.mathworks.com/help/images/examples/color-based-segmentation-using-k-means-clustering.html
May I ask if you tried this code? Or another code? Thanks a lot for your help
Walter Roberson
Walter Roberson 2018 年 4 月 5 日
I have not used that code; I do not work on image segmentation.
yuvraj singh
yuvraj singh 2018 年 12 月 20 日
imsegkmeans doesn't work
Walter Roberson
Walter Roberson 2018 年 12 月 20 日
imsegkmeans appears to have been introduced in R2018b in the Image Processing Toolbox. Are you using R2018b ?
Penny13
Penny13 2018 年 12 月 21 日
Yes.I work with MATLAB 2018 version.
Image Analyst
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');
0000 Screenshot.png
SRUTHI BANDI
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
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?

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by