File Exchange

image thumbnail


version (5.01 KB) by

Interactively segment RGB image into n user-defined clusters.



View License

[IDX,C,sumd,D] = clusterImg(imgin,nClusters,inMask)

This function implements kmeans clustering on an input RGB (m x n x 3) image. The user inputs at least two inputs: IMGIN and NCLUSTERS, and this function will step through an interactive color segmentation using kmeans clustering. It is interactive in that the user will be prompted to click-define the target colors in the segmentation. (There will be NCLUSTERS prompts.) Optionally, the user can also input a third argument containing a binary mask of the input image, in which case the program will operate on a subimage defined by the BOUNDING BOX of the (true) inMask.
Following segmentation (clustering), the user can EXPORT THE CLUSTERED IMAGE TO THE WORKSPACE, along with its corresponding colormap. Additionally, the user can opt to EXPORT A BINARY MASK OF A SINGLE COLOR CLUSTER TO THE WORKSPACE, or AUTO-GENERATE ALL MASKS. All of these options are initiated by press of a uicontrol pushbutton.

Comments and Ratings (8)


jithu (view profile)

thank u

Nam Le

Nam Le (view profile)

thank you

shoo chen

thank you ,you are very strong

li chuan-he

thanks you

Jiang Hu

Thank you for your code!

ed rams


smiled fisher

Bowo Lee

but, didn't explain to the beginner how to install the toolbox like image processing, statistic toolbox


Updated license


Updated M file and license text file copyright to The MathWorks, Inc.


updated license text file to The MathWorks, Inc.


added copyright

MATLAB Release
MATLAB 7.3 (R2006b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video