KMeans Points Clustering

Labeling non-connected components as a sinlge object using K-means clustering.



Segmented objects sometimes are undesirably discontinuous due to weak signal. If number of objects are already known, then we can use k-means clustering algorithm to label isolated dots as a single object, and this script is an implementation of such algorithm. It was written for 3D data set, but also works with 2D data.


The latest ImageJ + Apatch Commons Math.

Fiji already includes Apache Commons Math, so no need of installing additional library.


Download the script from its repository and then follow the instructions in Installing Scripts.



Simply running the code from either menu tree or with a direct run using Script Editor triggers a GUI to input two parameters, expected number of clusters and the number of iteration for computation.

IJ Macro

The script can be executed from ImageJ macro.

kmeansfunc = "/<path>/<to>/";
runMacro(kmeansfunc, "4 100");

<path>/<to> should be replaced with the path to the jython code in your local file system. The second argument of runMacro is the option values. The first one (“4” in the above) is the number of expected cluster. The second number is number of iterations (“10” in the above).

If the script is copied under /plugins/scripts/, the script appears within the menu tree. With this condition, the line below executes the script with the option values in the second argument.

 run("KMeans Points Clustering", "4 10");