File Exchange

image thumbnail

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

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

86 Downloads

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: "http://note.sonots.com/SciSoftware/NcutImageSegmentation.html". It is sensitive in choosing parameters.
an implementation by "Bryan Feldman" is used for “mean-shift clustering"

Comments and Ratings (30)

Hi,

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

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 ?

N V

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?

Chen Li

begginer01

begginer01

@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 work..it 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

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

Mathimage

Alireza

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

ceyhun

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

Hello Guys,

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

Error using matlab.graphics.axis.Axes/set
There is no Style property on the Axes class.

I am using R2015b..

Thanks

@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

VERY GOOD

Best image segmentation ever

krimou24

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 !

Jassica

Li Dan

Updates

1.0.0.0

FX submission added

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