4.2 | 53 ratings Rate this file 369 Downloads (last 30 days) File Size: 1.59 KB File ID: #8379
image thumbnail

kmeans image segmentation



28 Aug 2005 (Updated )

Application of kmeans clustering algorithm to segment a grey scale image on diferent classes.

| Watch this File

File Information

This program gets an image and the desidered number of partitions and finds the means of the different classes and provides a clasified image


This file inspired Tactics Toolbox.

MATLAB release MATLAB 6.5.1 (R13SP1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (85)
11 Dec 2014 Moe Myint

When I run the matlab code,i have error sounds like this "Input argument "k" is undefined."
in line 42...
pls help me....
Thanks a lot....

18 Nov 2014 nandini

i should apply kmeans for the background eliminated image please help me

31 Jul 2014 Nora

Why not show users how to visualize the result?

17 Jul 2014 hung huu  
06 Jun 2014 jiyo

Jessica please use imshow(mask,[]) and check. It should work

25 Jan 2014 Jessica

When I run the matlab code with my .jpg image nothing happens. I tried "imshow(mask)" but I only get a white image. What am I doing wrong ? Is there something to add to the code to get an image output with the result of the segmentation on my image ?

11 Nov 2013 sukhvir kaur

when i run this code then output of mask is red...why this is happening can anyone help me plz???

07 Oct 2013 shazwani kamarudin

when i run matlab code to test this segmentation code, i have error sounds like this "Input argument "k" is undefined."

in line 42...

help me please

13 Jun 2013 mangood UK

where is the segmentation image result??

28 Feb 2013 Raz Shimoni

Thank you.

22 Nov 2012 Javier


08 Nov 2012 Sourav

how do i get to see the code?

11 Oct 2012 parvathi menon

How do I implement this code?

12 Jul 2012 Nikolay S.

Works nicely.
A little word of advice, if I may...
By adding the following two lines (in lines 25-26) you will be capable of dealing non integer inputs also (matrix of double's in my case):
Consider performing rgb2gray to deal with color image inputs as well.

greyLevels=2^8-1; % Number of gray levels

Best regards, and keep the good work!

07 Jul 2012 Neha

@zheng~~~ plzz help if u r able to understand the code..

06 Jul 2012 zheng

very good

06 Jul 2012 zheng

very good

06 Jul 2012 Neha

where is the statement for loading image by using this code..?? :(

06 Jul 2012 Neha

plzz help me how to implement it... i need k-mean clustering deadly for my project work having deadline 15July...

21 Jun 2012 3omyer 3omayer

Excellent code, but please, I need to extract the pixels for each classes to calculating some statistical proprities, like mean or std. can you help me please !!

01 Jun 2012 Nidhi

How will we recover the image from the

01 Jun 2012 Nidhi

Can anyone help me in how to implement this function on image ??

30 Mar 2012 zhou join


15 Mar 2012 Aksam Iftikhar

Excellent code, but I came across a problem in this code. Whenever number of clusters specified are more than possible partitions of the image the algorithm got stuck. Here is the solution.

find following line in the given code
if(mu==oldmu) break;end;

now add following lines of code before this line.
for i=1:k,
mu(i) = i*m/(k+1);

The problem was occuring when NaN was encountered in centroid computation.

31 Jan 2012 lbrce


18 Nov 2011 Vipul  
27 Sep 2011 Saleh

The segmented image should be multi colors, based on number of clusters. But I have mask with only one color "always red", Why? I need the segmented image
Help me please...

27 Jun 2011 David Smith

Doesn't work for me. Tried to run it on the Shepp-Logan phantom:

>> [u,m]=kmeans(phantom(128),4);

??? Attempted to access h(1.2); index must be a positive integer or logical.

Error in ==> kmeans at 35
if(ima(i)>0) h(ima(i))=h(ima(i))+1;end;

27 Jun 2011 David Smith  
08 May 2011 ops OPS

i don't how its work with image !! do u have GUI ?

01 May 2011 Ahmed  
05 Feb 2011 osunmbs unduo  
13 Jan 2011 Vinkle kumar

good one..

29 Dec 2010 Anna

Can you pls tell me for which k=? did you obtain the result from the image?

22 Dec 2010 Ömer KAYA

there is a problem if there is a total range of grey levels in the image then it is going to be a kind of problem with the solution.

The ımage is complete white?

What shall we do/do not about it?

Nice code...

21 Dec 2010 Rekha G R  
25 Nov 2010 sanny

mahmoud can yo send me the right code?

05 Oct 2010 spectral

>Mahmoud Shehadeh I'm gonna send you an email.

k..tq..does it works on the grayscale image..

05 Oct 2010 Mahmoud Shehadeh

I'm gonna send you an email.

04 Oct 2010 spectral

'Mahmoud'..do you have the corrected code

01 Oct 2010 Mahmoud Shehadeh

sorry that was not the right code :)

01 Oct 2010 spectral

anyone can help me..why i didn't successfully execute the results..
i'm using the jpeg image.tq

26 Sep 2010 Mahmoud Shehadeh  
03 Jul 2010 Nitin  
17 Jun 2010 Liu Zhidan


30 Apr 2010 Seena Ghazi

trabajo bién

31 Mar 2010 Airballman

Works fine, thanks!

A bit long when used on high resolution images. But I guess it comes from the kmeans methods itself :)

11 Mar 2010 TRAN VietDung

nice !

08 Jan 2010 Bhanu

Please tell how to execute this code

07 Jan 2010 Bhanu

Please help me how to execute this code in matlab

07 Dec 2009 Aditya Gupta

Thanks, works good.

24 Nov 2009 Tai Fei


20 Jul 2009 Assaf Zaritsky

Small bug at line#73, should be:
instead of:
c=abs(copy(i,j)-mu); % (mi missing)

Also, performance can be quite improved

27 May 2009 hbu 

it is very beauteful,excellent!


I want to apply kmeans for landsat images pls help me for dat

13 Mar 2009 kiruba mani

im doing my final year project in image segmentation based on MRF with ANT COLONY SYSTEM,i dont have any idea ab't that,can u help me,my first module is k-means,can u explain k-mans in depth.

03 Oct 2008 Patirniche Dinu  
08 Aug 2008 Abirami Balasubramaniyan


30 May 2008 mcasarah tucker  
27 May 2008 fd fdafd


20 May 2008 Ghassan alyami  
09 May 2008 Chris Yang

please help me, how to use this code

19 Feb 2008 Gustavo Meschino

Very fast implementation!
It would be great if it is improved to enter more than one input image.

05 Feb 2008 Adrian López  
23 Jan 2008 manoj kumar


22 Jan 2008 Santhosh Kumar

I need a help from u to solve a paper in image processing

15 Jan 2008 AYEB YAKOUB

this function works only for integer values of gray level, not for double

15 Aug 2007 smiled fisher  
02 Aug 2007 Volker E.  
03 May 2007 Yurniar Supanggi

Good work, this code very useful for me, but I need method hierarchical clustering for image..., Any idea for me?

23 Nov 2006 John Thebridge  
21 Oct 2006 oscar forero  
07 Sep 2006 Mikel Rodriguez (www.cs.ucf.edu/~mikel)

No errors, well done. For some applications you might want to use Jose Vicente Manjon-Herrera's EM image segmentation implementation though.

10 Aug 2006 Elena Cher

workes good

11 Jul 2006 mohammad sadeghzadeh  
15 May 2006 Milind Potdar  
30 Apr 2006 Raj Pai Panandiker  
02 Apr 2006 cool coolleen

i m doing my final year project of mini portions segmentation method in face recognition using matlab. may i know how to put my cropped small portions of the face into matrix (eg: 6x9 matrix, where 6 refer to number of photos, n 9 refer to the number of small portions). by the way, i also would like to know how can i compare the matrix column by column in order to get the eigenvalues. n i m new to matlab, so i hope u will help me to clear my doubts. if u have to source code, pls send a copy to me. thanks and really appreciate for your help. Hope can hear from u soon.

01 Apr 2006 kosala jayakody  
01 Mar 2006 Davor Conejo

Just Perfect. Very fast and great code.

13 Feb 2006 Jean-Paul Kouma

Very good and fast!

13 Jan 2006 Xin Huang

K-means is a classic un-supervised classification algorithm. In your code, is there new ideas about k-means? And can you compare the k-means,ISODATA,SOM mathods?

13 Nov 2005 Stanley Zietz

Excellent. Works well.

25 Oct 2005 Bashar Nusir  
17 Sep 2005 Ramir Sokurka


Contact us