IJ Plugins: k-means Clustering

k-means Clustering plugin performs pixel-based segmentation of multi-band images. Each pixel in the input image is assigned to one of the clusters. Values in the output image produced by the plugin represent cluster number to which original pixel was assigned.

An input image stack can be interpreted in two ways. Slices can be interpreted as bands in a 2D image or as a 3D image. For instance, an RGB color images has three bands: red, green, and blue. Each pixels is represented by an n-valued vector, where n is a number of bands, for instance, a 3-value vector [r,g,b] in case of a color image.

Each cluster is defined by its centroid in n-dimensional space. Pixels are grouped by their proximity to cluster's centroids. Cluster centroids are determined using a heuristics: initially centroids are initialized using the k-means++ algorithm and then their location is interactively optimized. For more information on this and other clustering approaches see:

Anil K. Jain and Richard C. Dubes, Algorithms for Clustering Data, Prentice Hall, 1988. (PDF version available).

The main plugin k-means Clustering takes an input image and segments it based on clusters discovered in that image. Utility plugin k-means Clustering Reapply can use centers cluster computed for one image and use them to segment another image of the same type (image size can be different).

k-means Clustering Plugin options

  • Number of clusters - Number of segments image will be divided into.

  • Cluster center tolerance - At each iteration cluster center location are updated. If cluster centers, between interactions, move less than the tolerance value it ts assumed the algorithm converged to the final solution.

  • Interpret stack as 3D - If checked the input stack is interpreted as a 3D image. If not checked, the input image is assumed to be 2D with number of values per pixel equal to the number of slices.

  • Enable randomization seed - When randomization seed is used, cluster centers are initialized to the same values every time algorithm starts. When randomization seed is disabled cluster center will be initialized differently each time. It is possible that different cluster initialization may lead to different final solutions.

  • Randomization seed - The seed is the initial value of the internal state of the pseudorandom number generator.

  • Show clusters as centroid value - produces additional output image where clusters are represented by its centroid value, see examples below.

  • Enable clustering animation - produces additional output showing optimization process.

  • Print optimization trace - prints out cluster centroids and change in centroid location at each iteration.

  • Send to results table - Computed cluster centers are send to the results table. This can be used in combination with k-means Clustering Reapply plugin to cluster more images in the same way.

k-means Clustering Reapply Plugin options

  • Table with clusters - Result table containing cluster centers created by k-means Clustering plugin.

  • Image to apply clusters - Image that you want to segment. It should be of the same type as the image used to create clusters. Sizes can be different

  • Interpret stack as 3D - In version 1.9 this option is not yet supported.

  • Show clusters as centroid value - produces additional output image where clusters are represented by its centroid value, see examples below.

Clustering Segmentation Examples

4-class k-means segmentation of a color image:

kdf_black kdf_black_4_clusters kdf_black_4_cluster_centroids
Original. Clusters encoded by gray level. Clusters encoded by centroid color.

3-class k-means segmentation of a color image

flamingo flamingo clusters flamingo centroids
Original. Clusters encoded by gray level. Clusters encoded by centroid color.

Possible Extensions of the Current Implementation

  • Processing only within ROI

  • Anisotropic distance measures, for instance, Mahalonobis

  • Extension to fuzzy k-means

  • Cluster validity indexes

  • Automatic selection of the number of clusters

The k-means Clustering plugins installs in ImageJ under: Plugins/Segmentation/k-means Clustering.

Download and Installation

The k-means Clustering plugin is part of ij-Plugins Toolkit. Download and installation are described here