Image segmentation using k means clustering
9 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Image Analyst
on 20 Dec 2018
Edited: Image Analyst
on 20 Dec 2018
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.
More Answers (1)
Walter Roberson
on 4 Apr 2018
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 Comments
SRUTHI BANDI
on 22 Dec 2019
Error in demo1 (line 16)
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
error comes like this
Image Analyst
on 22 Dec 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?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!