File Exchange

image thumbnail

Fast kmeans Algorithm Code

version 1.7.0.0 (2.36 KB) by ankit dixit
A Very fast and efficient Implementation for kmeans clustering of an Image or Array.

19 Downloads

Updated 10 Jan 2014

View Version History

View License

This code uses MATLAB's Internal Functions and Memory Preallocations to apply a Fast Implementation of kmeans algorithm. This is a efficient code for clustering a gray or Color image or it can be used for clustering a Multidimensional Array.

Comparison.
1. Faster than MATLAB's internal kmeans function.
2. Consistant Output than internal kmeans.
3. 100% convergence.
4. Very efficient for color image segmentation in L*a*b* color space.
5. Very easy to understand and can be easily modified according to requirement.

Hope you will like it. i am waiting for your reviews and comments.

Cite As

ankit dixit (2020). Fast kmeans Algorithm Code (https://www.mathworks.com/matlabcentral/fileexchange/44598-fast-kmeans-algorithm-code), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

W W

W W

AA

W W

Tan Chik Yong

Example:
Img = imread('Example.BMP'); %Example.BMP is RGB
Img = rgb2gray(Img);
k = 2;
[mu,mask]=kmeans_fast_Color(Img,k);
mu=mat2gray(mu);% convert matrix to image
imshow(mu);

Sun Rover

The reason why this function is faster then MATLAB's internal k-means function might be that the author restrict the starting point strategy. But, since we know that k-means is greatly influenced(or determined) by the starting point. This function might got trapped in the local optimum and there is no way to get out from there in this function. Still, this function is easy to use and help somebody to deal with reshaping multi=dimensional data.

Rahul Ghongade

Thank You sir for great submission, please tell me how to use it for breast cancer segmentation

Sina

Jishnu C Rajan

very thankful to you.
i was working on the image of a brain with tumor. but the output contains some kind of white lines along with the tumor portion. How can I avoid that unwanted portions...???

ankit dixit

@Irenicus, nice explanation, Thank you

Irenicus

@arjun:

The following code divides a given image into N clusters, and save each cluster in 'images' cell array.

Have Fun

I =imread('...'); % choose some image
N = 3; % Choos number of clusters
[label_im,vec_mean] = kmeans_fast_Color(I,N);
images = cell(1,N); % Crate images array

rgb_label_im = repmat(label_im,[1 1 3]);

for i=1:N
temp = rgb_label_im;
temp(temp~=i) = 0; %Clear all other clusters other than i
images{i} = I.*uint8(temp); % Save cluster
end

arjun

@ankit, how to use this code to see the output clustered images?.. thanks in advance...

Irenicus

@ankit dixit, I actually went and modified your code to work with CIEDE2000 metrics. And while the improvement in clustering is debatable, the run time increased significantly, so I guess that Euclidean distance is the way to go in most cases.
Thanks for your efficient code, I would never have been able to run a modified Matlab function (I tried running it...).

ankit dixit

@Irenicus, you can use other distance metric also like Mahalanobis or City block or you can use a normal distribution and calculate minimum distance using probability distribution, it depends on complexity of your data, if your data is very nonlinear go for different distances, for keeping things simple i have used only a euclidean metric for distance estimation. Thank You

Irenicus

In case of L*A*B* clustering, I 'm not sure, but I think that the correct metrics are not simply euclidean (CIEDE2000).

http://www.ece.rochester.edu/~gsharma/ciede2000/

Pappu Murthy

Excellent code. Worked like a charm.

John

Apeksha Jain

JOHN

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

Inspired: Sparsified K-Means

Community Treasure Hunt

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

Start Hunting!