4.1 | 56 ratings Rate this file 248 Downloads (last 30 days) File Size: 1.59 KB File ID: #8379 Version: 1.0
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 (97)
30 Apr 2015 AZama

AZama (view profile)

Please could anyone tell me how this code works?
Thanks in advance.

Comment only
21 Apr 2015 Vasumathi Ganesh

Hello sir, I'm getting this error while running your code
Error using kmeans (line 20)
Not enough input arguments.

Comment only
04 Mar 2015 Nuno

Nuno (view profile)

How can i define k?

Comment only
26 Feb 2015 Tushar

Tushar (view profile)

How to get the segmented image?

Comment only
18 Feb 2015 marjan

marjan (view profile)

08 Feb 2015 fatma

fatma (view profile)

hello,i am realy poor in matlab coding and i dont know how to overcome it.Kmeans in image processing is my homework,too.Still i dont know how i must progress in this:S Please help me

Comment only
24 Jan 2015 Mark Toolan

Aswin Raj:

Try 'k=str2double(k);' just before line 46

Comment only
23 Jan 2015 Aswin Raj

Error using kmeans (line 46)
Not enough input arguments.
L46 : mu=(1:k)*m/(k+1);

how to overcome this error??

Comment only
18 Jan 2015 naipunya pradhan

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 ?

Comment only
14 Jan 2015 Maria

Maria (view profile)

Oh, I may have missed something, but I may be worth adding a line of say:


on line 33 before the first loop in case you have a matrix of non-integer values.


Comment only
14 Jan 2015 Maria

Maria (view profile)

Nice, simple, elegant. Cheers.

27 Dec 2014 sahar Pordeli Behrouz  
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....

Comment only
18 Nov 2014 nandini

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

Comment only
31 Jul 2014 Nora

Nora (view profile)

Why not show users how to visualize the result?

17 Jul 2014 hung huu  
06 Jun 2014 jiyo

jiyo (view profile)

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

Comment only
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 ?

Comment only
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

Comment only
13 Jun 2013 mangood UK

where is the segmentation image result??

Comment only
28 Feb 2013 Raz Shimoni

Thank you.

22 Nov 2012 Javier

Javier (view profile)


08 Nov 2012 Sourav

Sourav (view profile)

how do i get to see the code?

Comment only
11 Oct 2012 parvathi menon

How do I implement this code?

Comment only
12 Jul 2012 Nikolay S.

Nikolay S. (view profile)

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

Neha (view profile)

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

Comment only
06 Jul 2012 zheng

zheng (view profile)

very good

06 Jul 2012 zheng

zheng (view profile)

very good

06 Jul 2012 Neha

Neha (view profile)

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

Comment only
06 Jul 2012 Neha

Neha (view profile)

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

Comment only
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 !!

Comment only
01 Jun 2012 Nidhi

Nidhi (view profile)

How will we recover the image from the

Comment only
01 Jun 2012 Nidhi

Nidhi (view profile)

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

Comment only
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

lbrce (view profile)


18 Nov 2011 Vipul

Vipul (view profile)

27 Sep 2011 Saleh

Saleh (view profile)

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...

Comment only
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;

Comment only
27 Jun 2011 David Smith  
08 May 2011 ops OPS

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

Comment only
01 May 2011 Ahmed

Ahmed (view profile)

05 Feb 2011 osunmbs unduo  
13 Jan 2011 Vinkle kumar

good one..

Comment only
29 Dec 2010 Anna

Anna (view profile)

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

Comment only
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

sanny (view profile)

mahmoud can yo send me the right code?

Comment only
05 Oct 2010 spectral

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

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

Comment only
05 Oct 2010 Mahmoud Shehadeh

I'm gonna send you an email.

Comment only
04 Oct 2010 spectral

'Mahmoud'..do you have the corrected code

Comment only
01 Oct 2010 Mahmoud Shehadeh

sorry that was not the right code :)

Comment only
01 Oct 2010 spectral

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

Comment only
26 Sep 2010 Mahmoud Shehadeh  
03 Jul 2010 Nitin

Nitin (view profile)

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

Bhanu (view profile)

Please tell how to execute this code

07 Jan 2010 Bhanu

Bhanu (view profile)

Please help me how to execute this code in matlab

Comment only
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 

hbu  (view profile)

it is very beauteful,excellent!

Comment only

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

Comment only
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.

Comment only
03 Oct 2008 Patirniche Dinu  
08 Aug 2008 Abirami Balasubramaniyan


30 May 2008 mcasarah tucker  
27 May 2008 fd fdafd


Comment only
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.

Comment only
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?

Comment only
13 Nov 2005 Stanley Zietz

Excellent. Works well.

25 Oct 2005 Bashar Nusir  
17 Sep 2005 Ramir Sokurka


Contact us