File Exchange

image thumbnail

kmeans image segmentation

version 1.0 (1.59 KB) by

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

4.03333
61 Ratings

131 Downloads

Updated

View License

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

Comments and Ratings (114)

Veronika

I defined k before running the code and put ima=round(ima); before line 32, but still any output, MATLAB is still "busy".

Aggies

Aggies (view profile)

I am a beginner and I am confused how should I interpret the outputs (numbers), is it possible to visualise it on the input image? anyone?

Waqas Ahmad

befreo running the code one has to define k i.e. the number of clusters, put ima=round(ima); somewhere before line 32 to round the m value and define a proper image to load

Vaishnuvi V

Why no output displayed? How to write code for it along with this current code?

nirmala

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

plz help me .how to see the segmented image

ziggy

ziggy (view profile)

Hi, does anyone know what type of distance measure is being used in this version of k-means?
I see the
c=abs(ind(i)-mu);
within the "current classification" part, is that Manhattan distance?

Also, is
mu=(1:k)*m/(k+1);
the standard way of calculating the centroids? I cannot seem to find a corresponding formula.
Thanks!

rahul verma

This code is running fine, without o/p :-(

but i tried
imshow(mask,[])
imshow(mask)

i didn't get any output.
How to get the segmented image?

Karla Palma

how can i see the files??

wei li

wei li (view profile)

javeria amin

when i run matlab code to test this segmentation code, i have error sounds like this "Input argument "k" is undefined.How can i define k?"plz help me

javeria amin

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

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 ?

does it return the k centroids from the kmeans clustering as classes?

please help me to see the output.

can u tell how to see this output.Am getting problem while running code..please help me please thanks in advance

Qian

Qian (view profile)

why i cant see the code?

AZama

AZama (view profile)

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

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

Nuno

Nuno (view profile)

How can i define k?

Tushar

Tushar (view profile)

How to get the segmented image?

marjan

marjan (view profile)

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

Mark Toolan

Aswin Raj:

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

Aswin Raj

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

how to overcome this error??

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 ?

Maria

Maria (view profile)

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

ima=round(ima)

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

thanks

Maria

Maria (view profile)

Nice, simple, elegant. Cheers.

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

nandini

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

quix

quix (view profile)

Why not show users how to visualize the result?

hung huu

jiyo

jiyo (view profile)

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

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 ?

sukhvir kaur

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

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

mangood UK

where is the segmentation image result??

Raz Shimoni

Thank you.

Javier

Javier (view profile)

Excellent!

Sourav

Sourav (view profile)

how do i get to see the code?

How do I implement this code?

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
ima=round(greyLevels*ima/max(ima));

Best regards, and keep the good work!

Neha

Neha (view profile)

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

zheng

zheng (view profile)

very good

zheng

zheng (view profile)

very good

Neha

Neha (view profile)

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

Neha

Neha (view profile)

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

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

Nidhi

Nidhi (view profile)

How will we recover the image from the
matrix??

Nidhi

Nidhi (view profile)

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

zhou join

rubbish

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,
if(isnan(mu(i)))
mu(i) = i*m/(k+1);
end
end

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

lbrce

lbrce (view profile)

xddfd

Vipul

Vipul (view profile)

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

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;

David Smith

ops OPS

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

Ahmed

Ahmed (view profile)

Vinkle kumar

good one..

Anna

Anna (view profile)

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

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

Rekha G R

sanny

sanny (view profile)

mahmoud can yo send me the right code?

spectral

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

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

I'm gonna send you an email.

spectral

'Mahmoud'..do you have the corrected code

sorry that was not the right code :)

spectral

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

Nitin

Nitin (view profile)

Liu Zhidan

good

Seena Ghazi

trabajo bién

Airballman

Works fine, thanks!

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

TRAN VietDung

nice !

Bhanu

Bhanu (view profile)

Please tell how to execute this code

Bhanu

Bhanu (view profile)

Please help me how to execute this code in matlab

Aditya Gupta

Thanks, works good.

Tai Fei

Dank!

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

Also, performance can be quite improved

hbu 

hbu  (view profile)

it is very beauteful,excellent!

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

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.

Patirniche Dinu

Abirami Balasubramaniyan

good

mcasarah tucker

fd fdafd

good

Ghassan alyami

Chris Yang

please help me, how to use this code

Gustavo Meschino

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

Adrian López

manoj kumar

helpful

Santhosh Kumar

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

AYEB YAKOUB

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

smiled fisher

Volker E.

Yurniar Supanggi

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

John Thebridge

oscar forero

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.

Elena Cher

workes good

mohammad sadeghzadeh

Milind Potdar

Raj Pai Panandiker

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.

kosala jayakody

Davor Conejo

Just Perfect. Very fast and great code.

Jean-Paul Kouma

Very good and fast!

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?

Stanley Zietz

Excellent. Works well.

Bashar Nusir

Ramir Sokurka

Excellent!

MATLAB Release
MATLAB 6.5.1 (R13SP1)
Acknowledgements

Inspired: TACTICS Toolbox

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video