Code covered by the BSD License  

Highlights from
KMeans Segmentation - MEX

4.33333

4.3 | 3 ratings Rate this file 30 Downloads (last 30 days) File Size: 5.7 KB File ID: #27969

KMeans Segmentation - MEX

by Ahmad

 

21 Jun 2010 (Updated 23 Jun 2010)

Given N data elements of R dimensions (N x R matrix), it segregates the n elements into k clusters

| Watch this File

File Information
Description

KMEANSK - mex implementation (compile by mex kmeansK.cpp
Also an equivalent MATLAB implementation is present in zip file

Performs K-means clustering given a list of feature vectors and k. The argument k indicates the number of clusters you want the data to be divided into. data_vecs (N*R) is the set of R dimensional feature vectors for N data points. Each row in data_vecs gives the R dimensional vector for a single data point. Each column in data_vecs refers to values for a particular feature vector for all the N data points. The output data_idxs is a N*1 vector of integers telling which cluster number a particular data point belongs to. It also outputs centroids which is a k*R matrix, where each rows gives the vector for the cluster center. If we want to segment a color image i into 5 clusters using spacial and color information, we can use this function as follows:

% r = i(:,:,1);
% g = i(:,:,2);
% b = i(:,:,3);
% [c r] = meshgrid(1:size(i,1), 1:size(i,2));
% data_vecs = [r(:) g(:) b(:) r(:) c(:)];
% [ data_idxs centroids ] = kmeansK( data_vecs, k );
% d = reshape(data_idxs, size(i,1), size(i,2));
% imagesc(d);

MATLAB release MATLAB 7.9 (2009b)
Other requirements MEX Compiled using Microsoft Visual C++ 2008 SP1
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
23 Jun 2010 Joao Henriques  
23 Jun 2010 Joao Henriques

Looking good! The utilities file was a nice surprise. :)

03 Jul 2010 Mitsu Yamakado

The code make my matlab crash because of memory violation.
I am using mac os X, and apparently mxUNIT32_CLASS matlab type do not fit standard unsigned long c type. So I changed "data_idxs" into double* type rather than unsigned long* and everything works fine. Btw we one should use this code rather than the matlab kmeans function? Your code doesnt look to go faster or bring any more options.

03 Jul 2010 Ahmad

Thanks Mitsu. I'll make that fix.

Plus the kmeans comes with the statistics toolbox, which I didn't have access to. So I thought might be useful for other ppl.

10 Nov 2010 Ralu

Very useful for my grayscale experiments!

Please login to add a comment or rating.
Updates
21 Jun 2010

Included some input checking!

23 Jun 2010

Comments update. Plus shifted some functionality to a utils file

Tag Activity for this File
Tag Applied By Date/Time
kmeans Ahmad 21 Jun 2010 12:01:29
segmentation Ahmad 21 Jun 2010 12:01:29
clustering Ahmad 21 Jun 2010 12:01:29
kmeans YIMING 08 Jan 2011 06:02:56
segmentation mumtaz 05 Feb 2011 15:19:16

Contact us at files@mathworks.com