File Exchange

image thumbnail

k-means, mean-shift and normalized-cut segmentation

version (25.1 KB) by Alireza
k-means, mean-shift and normalized-cut segmentation


Updated 27 Aug 2015

View License

This code implemented a comparison between “k-means” “mean-shift” and “normalized-cut” segmentation
Teste methods are:
Kmeans segmentation using (color) only
Kmeans segmentation using (color + spatial)
Mean Shift segmentation using (color) only
Mean Shift segmentation using (color + spatial)
Normalized Cut (inherently uses spatial data)
kmeans parameter is "K" that is Cluster Numbers
meanshift parameter is "bw" that is Mean Shift Bandwidth
ncut parameters are "SI" Color similarity, "SX" Spatial similarity, "r" Spatial threshold (less than r pixels apart), "sNcut" The smallest Ncut value (threshold) to keep partitioning, and "sArea" The smallest size of area (threshold) to be accepted as a segment

an implementation by "Naotoshi Seo" with a little modification is used for “normalized-cut” segmentation, available online at: "". It is sensitive in choosing parameters.
an implementation by "Bryan Feldman" is used for “mean-shift clustering"

Cite As

Alireza (2020). k-means, mean-shift and normalized-cut segmentation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (31)

maneli alexand


Thanks a lot for the code. Is there any chance that you have this code for 3d Point cloud as well?

Anamitra Datta

Kaustubh Waghavkar



Thanks for your code. I would like to ask how can we get locations of cluster's centroid after mean shift segmentation.

Best regards.

anna st

lexxy rule

yousfi ihssane

hello , thank you for your code
please ,what must i add to this code if i want to show the centroid of clusters in the image ?


Wei Chen

Good, easy to read and use.

Nathan Zhang

what is the significance of this bound box you are calculating, it has no use for further implementation?

danesh danesh

Chen Li



@Alireza...Hello thank you for this code...Can you plz tell me..will this work for grayscale images. I run this code on gray image but it doesnot shows error:
Error using reshape
To RESHAPE the number of elements must not change.
Error in Km (line 5)
F = reshape(I,size(I,1)*size(I,2),3); % Color Features

How can i use this method for gray image? Plz help me..

dahlia urb

First off, thank you for posting this.

Secondly, I'd like to point out that when using spatial and color information as features you're normalizing the spatial information by performing:
L = [y(:)/max(y(:)),x(:)/max(x(:))];
Which is by itself a very valid operation if you want to introduce spatial information to some normalized features-space.

But my problem is that no such normalization is done on the color channels:
C = reshape(I,size(I,1)*size(I,2),3);

And I would like to suggest an improvement when using color information, use the L*a*b* color space:
lab_I = rgb2lab(I);
C = reshape(lab_I(:,:,2:3)./max(max(lab_I(:,:,2:3))),size(I,1)*size(I,2),2);

In this code, there is an array of cluster votes at the end of all iterations (when all pixels are associated with some cluster).

ClusterVotes has pixels as columns and clusters as rows where values are votes for each cluster for a given pixel. The author then associate pixel with that cluster with which it has more votes.

Now, I can't understand this thing.

For example, say the pixel has 10 votes for cluster 1 and 3 votes for cluster 2.

By author's logic, pixel will be associated to cluster 1.

But maybe the 10 votes that are associated to cluster 1 was given at the start of iterations and this pixel went out of bandwidth near to end of iterations.

Similarly, maybe the cluster 2 got its 3 votes near the end of mean-shift iterations that is when this pixel was indeed in the bandwidth and when mean stopped shifting.

So in such cases pixel should be associated to cluster 2.



@Luu Thanh, the current implementations are not the most efficient ones. They have rather used to proof concepts.


is any citation for this code (K-Means color+spatial) etc.

Haben Yhdego

Hello Guys,

I got the the ff problem when I run the code.

Error using
There is no Style property on the Axes class.

I am using R2015b..


santosh reddy

Kedar Prabhudesai

@Luu. Although K-means is faster, it only gives spherical shaped clusters. Mean shift is more robust compared to K-means as it does not assume any shape of clusters, see paper by Comaniciu and Meer for more details.

jia zheng


Dae Hyeon Gwon

Best image segmentation ever


Luu Thanh

hello Sir, I hear that K-mean (O(kn)) is faster than MeanShift (O(kn*n)). So why when I used your algorithm, MeanShift alway run faster ? Thank you !


Li Dan

ahmed ismail

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

Community Treasure Hunt

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

Start Hunting!