How to get segmented image using k-means clustering ?
Show older comments
Greetings to the members of the community!
I am trying to segment a grayscale image of size m-by-n using k-means clustering ( k = 3, in this case). In this attempt, I am using the normalized values of the pixel coordinates and intensity levels as the coordinates in the feature space to perform k-means clustering.
For e.g. for a pixel located at (x,y) having an intensity of I(x,y) the coordinate of that data point in the feature space is (x/m, y/n, I(x,y)/max(image))
Once the clustering is completed that is the centroid values of the clusters no longer change their coordinates drastically (checked by setting a threshold value) from their positon in the previous iteration, the pixels corresponding to the data points are re-allocated with a new intensity value.
However, when I run the program, the overlayed image doesn't match that obtained from using the in-built function imsegkmeans.The attached code is specific to k = 3. I am not able to figure out what piece I am missing or doing incorrectly.
Please share your valuable suggestions on how do I perform the clustering operation correctly.
Thanks!
Answers (1)
Image Analyst
on 12 Nov 2024
0 votes
I can't follow your code without putting a lot more time into it. There are not enough comments. It looks like you're trying to write your own algorithm for kmeans instead of using the built-in kmeans functions.
In general kmeans is not usually a good algorithm for grayscale segmentation, and certainly not for the cameraman image which has a broad continuum of gray levels.
In addition there is a special kmeans for images: imsegkmeans
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!