File Exchange

image thumbnail

Mean Shift Clustering

version (2.66 KB) by Bart Finkston
Cluster data by using the Mean Shift Algorithm


Updated 27 Feb 2006

No License

Clusters data using the Mean Shift Algorithm. testMeanShift shows an example in 2-D. Set plotFlag to true to visualize iterations.

Cite As

Bart Finkston (2021). Mean Shift Clustering (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (52)

Joseph Kirk

This function does not return the correct values for the cluster means --- if you compare the output values to recomputed means using the points in each cluster, you will get different values

Image Analyst

Please upload a screenshot showing the original image plus the filtered image so people can understand better what this program will do.

Jia Eckhard

To make it work for 1D input (grey level image):
line 47, change
sqDistToAll = sum((repmat(myMean,1,numPts) - dataPts).^2)
sqDistToAll = sum((repmat(myMean,1,numPts) - dataPts).^2,1)



Jihang Wang

Not working for 1D data input. Especially when I try to segment gray scale image.

Jing Kai Toh

Is it possible converting this code into a .mex for speeding up?

Pan Kelvin

it works very well

yu liu

Filippos Markolefas



I have tested this code on my data and found that i have 8 clusters but i have 7 labels , how can that be valid?

Please Inform me.


I have tested this code on my data and found that i have 8 clusters but i have 7 labels , how can that be valid?

Please Inform me.

Jeyshri j

while running this code..
Causing error like this

Error in testMeanShift (line 25)
[clustCent,point2cluster,clustMembsCell] = MeanShiftCluster(x,bandwidth);


How to use this to cluster 3D points?




I notice that there's some randomness to this implementation, anyone knows what parts of the method is causing the randomness?


Hi, I am trying to use meanshiftclustering for find clusters within a matrix of m (features) x n (images). Anyone know why this makes a grouping of thousands of pictures and then groups of just a picture? thanks
despite increasing the value of BW is going the same.

M = 105x3959

[clustCent, CdecadaP, PsdecadaC] = MeanShiftCluster (M, 0.8);
  >> Psdecadac = 2126 x 1 >> {1,1} = 767 elements


meaning shift is OK?

Henrik Skovsgaard

Punit Rathore

Can anybody give me the this code for colour image segmentation??

Peter K

This is great code! It's very easy to read and taught me a lot about mean shift.

I found that the runtime was decreased on my machine and data by replacing the lines:

sqDistToAll = sum((repmat(myMean,1,numPts) - dataPts).^2);
beenVisitedFlag(myMembers) = 1;


sqDistToAll = sum(bsxfun(@minus,myMean,dataPts).^2);
beenVisitedFlag(inInds) = 1;


Thanks for the code!

Peter K


Guys, is there any code for high dimensional data? This code doesnt work for D is greater than 2. Thanks.


In order to Bart's code on 1D dim data a small change should be made:

sqDistToAll = sum((repmat(myMean,1,numPts) - dataPts).^2,1);

Harrison Woods

Very good code, thank u ,guy!

Fuat Cogun

Anton Kylling

Problem solved :)

Anton Kylling

I get an unhandled exception using edison_wrapper. Anyone else experiencing this?

MATLAB Version: (R2008b)
Operating System: Microsoft Windows Vista
Window System: Version 6.0 (Build 6002: Service Pack 2)
Processor ID: x86 Family 6 Model 7 Stepping 6, GenuineIntel
Virtual Machine: Java 1.6.0_04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Default Encoding: windows-1252

Vista x64 might be the problem, but I have recompiled the wrapper.

Any help is appreciated. Thanks in advance.


>> doc edison_wrapper
and you'll get a description of the various parameters and default values.

Luis Mont Blanc

I see this:
But i don't understand how to use it.
How can i use the 4th argument of the function?

Can you please write a sample code of how to use it?

Thank you

Sebastien PARIS

Well done!

brais martinez

good code and working straight away but there is no option to set weights to the clustered points

also a brief help file would be useful (to describe parameters and so on)

Yun Yongsheng

I have downloaded this code, and tested in my pc'matlab6.5 R13. But there are still two errors happened. The error is reported as " The Error in ==> C:\MATLAB6p5\work\RectifKitU\MeanShift\MeanShiftCluster.m
On line 99 ==> clusterVotes(numClust,:) = thisClusterVotes; "
??? Subscripted assignment dimension mismatch."

I don't know how to solve this problem for the code. Could any one kind help me?

Wahyu Caesar

can I use to vibration data.
please send the answer to my email :

Daniel Carlton

Rob, by padding you mean adding a row containing only zeros ? Indeed using a 1D vector (a grayscale image vectorized) the code crashes as you say.

What does the "bandwitdh" parameter stand for ? Is it the window size ?

Thank you

Rob Barton

It is good base code:

However I believe that the author is using something closer to a nadaraya-watson estimator (When he takes the:

Mean = mean(dataPts:,inInds),2);

He states he is using a 'flat' or epanechnikov kernal and the computation is different:

See: Dorin Comaniciu "Mean Shift: A Robust Approach Toward Feature Space Analysis" IEEE Transactions on pattern analysis and machine intelligence, Vol. 24, No. 5, May 2002.

By altering this and making a few other minor edits this is excellent code to start from to customize to the users requirements.

the computation of 'sqDistToAll = sum(...)' makes the assumption that the user passes more than a 1D data... so if you passed just gray levels of an image in a 1D array the code will crash, however if you passed a LUV image, (3 elements per pixel) it would work. alternately you can pad the input data with a redundant row or alter the code to check for a single row input and perform the slightly modified but same computation.


Tac Charles

Thank you

jaya priya

i want to do it for color image segmentation how to do pls help me

Kim GH

It's good but some problem occures when running on the matlab ver 6.5.
To aboide this modify like that
"zeros(1,numPts,'unit8');" -> "zeros(1,numPts);"

Patson patrick

I would like to subscribe the algorithm mean shift .
Tank you

praisan padungweang

clustering algorithm on images?
You may prepare the data in 1D(gray scale), 3D (RGB or x,y,I;in gray scale) or 5D (x,y,R,G,B) then go with mean shift.

Ming Liu

The code is good, while more examples can further improve the quality.

JW Lee

Can anyone provide some help for me on how to use this clustering algorithm on images?

satria gunawan

i too not undestand to used to image, there are some body help me?

hosung han


Ning J.

see D. Comaniciu PH.D Thesis about clustering or applying mean shift to image!

Hanh Vu

Hello every body
I have to use the mean-shift algorithm for finding the modes of a
image histogram. is there anyone that have an M-file to do this?
Thanks in advance.

Abhijeet Jadhav

How to use it for image?

orazio gambino

Hi guys,

I changed the following lines into MeanShiftCluster.m and now it works:

40: beenVisitedFlag = zeros(1,numPts);
42: clusterVotes = zeros(1,numPts);
51: thisClusterVotes = zeros(1,numPts);


yang Min

very good

Yang wool

??? Subscripted assignment dimension mismatch.

Error in ==> D:\¹êÅç\MeanShift\MeanShiftCluster.m
On line 99 ==> clusterVotes(numClust,:) = thisClusterVotes;

Error in ==> D:\¹êÅç\MeanShift\testMeanShift.m
On line 25 ==> [clustCent,point2cluster,clustMembsCell] = MeanShiftCluster(x,bandwidth);

MATLAB Release Compatibility
Created with R14
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!