File Exchange

image thumbnail

K-means segmentation

version (11.9 KB) by Alireza
This code implements K-means color segmentation


Updated 27 Aug 2015

View License

Demo.m shows a K-means segmentation demo

K-means clustering is one of the popular algorithms in clustering and segmentation. K-means segmentation treats each imgae pixel (with rgb values) as a feature point having a location in space. The basic K-means algorithm then arbitrarily locates, that number of cluster centers in multidimensional measurement space. Each point is then assigned to the cluster whose arbitrary mean vector is closest. The procedure continues until there is no significant change in the location of class mean vectors between successive iterations of the algorithms.

Cite As

Alireza (2021). K-means segmentation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)

arezoo kbr

waseem ahmed

yao zhang

kowsalya ramaraj

kindly requesting friends. could any any one explain this code clearly?i didn't understand the operation in that.

Ana Campos

Hi, thank you.
Could you include a confusion matrix in the code? I would like to see the resutls and compare with different images.

Reece Murray

samaneh fakhri

Dragos Bratasanu

Aeolus Exe

what's the meangin of "NAN centers"


To compute the accuracy, you will need the ground truth for test images. By subtracting and computing the difference between each segment's pixel with groundtruth the error and accuracy can be computed.

Anis Mahmon

Hai Alireza thank you for it..
can you explain how to get the accuracy or plotting of image segmentation..I really hope u can help me, thanks

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: K-means clustering

Community Treasure Hunt

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

Start Hunting!