IPSDK 4.1
IPSDK : Image Processing Software Development Kit
K-means algorithm
classesImg,clustersCenters,compactnesskMeansImg (inImg,nbClusters)
classesImg,clustersCenters,compactnesskMeansImg (inImg,nbClusters,nbAttempts,nbMaxIterPerAttempt,centersShiftTol,outClassImg)
classesImg,clustersCenters,compactnesskMeansImg (inImg,inOptSingleGreyMaskImg,nbClusters)
classesImg,clustersCenters,compactnesskMeansImg (inImg,inOptSingleGreyMaskImg,nbClusters,nbAttempts,nbMaxIterPerAttempt,centersShiftTol,outClassImg)

Detailed Description

classifies pixels of an image using k-means algorithm

Note
Please, note that if the output image class is provided by the user, the class image field in the KMeansResults structure returned by the function points to this image. No copy is performed.

Classic K-means algorithm (LLoyd's algorithm) consists in clustering a set of n points of d dimensions in k clusters. Applied here to image processing, for an image of size (x, y, z, c, t) (with x, y and z the sizes respectively along x, y and z-axis, c the number of color channels and t the number of elements in temporal sequence), it will cluster the x*y*z pixels (for a 2d image, with z=1) or voxels (for a 3d image, with z>1) of c*t dimensions in k clusters, with k specified by the user.

The different steps are as follows:

For each attempt (number of attempts is specified by the user through the $InOptNbAttempts$ attribute):

Input and output attributes of the algorithm are:

Below is an example of the application of k-means algorithm on a RGB UInt8 image, with the following parameters: ( $InOptNbClusters$=4, $InOptNbAttempts$=1, $InOptNbMaxIter$=100, $InOptClustersCentersShiftTolerance$=0.01):

KMeansImg.png

The algorithm allows to classify only a subset of pixels/voxels by using a mask image as additional input. In this case, only the pixels/voxels with a mask value of True are classified and taken into account for the clusters calculation.

Here is an example of masked k-means based classification, using the same parameters as for the previous figure. Black pixels correspond to a value of False in the mask image:

KMeansImg_mask.png

References

[1] Stuart P. Lloyd. Least squares quantization in pcm. IEEE Transactions on Information Theory, 28(2):129–136, 1982.

Example of Python code :

Example imports

import PyIPSDK
import PyIPSDK.IPSDKIPLClassification as classif

Code Example

# opening of input image
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
nbClusters = 4
res = classif.kMeansImg(inImg, nbClusters)
outClassImg = res[0]
outClustersCenters = res[1]
outCompactness = res[2]
# Access to the first cluster center
firstClusterCenter = outClustersCenters.coll[0].elements

Example of C++ code :

Example informations

Header file

#include <IPSDKIPL/IPSDKIPLClassification/Processor/KMeansImg/KMeansImg.h>

Code Example

// load input image from file
ImagePtr pInImg = loadTiffImageFile(inputImgPath);
const ipUInt32 nbExpectedClusters = 4;
const ipUInt32 nbAttempts = 1;
const ipUInt32 nbMaxIterPerAttempt = 100;
const ipReal64 clustersCentersShiftTol = 0.1;
// apply k-means algorithm on input image
KMeansResults res = kMeansImg(pInImg, nbExpectedClusters);
// retrieve resulting class image
const ImagePtr pOutClassImg = res._pClassesImg;
// retrieve resulting clusters properties
const ClustersCentersPtr pOutClustersCenters = res._pClustersCenters;
// Access to the first generated cluster center elements
ClusterCenterPtr clusterCenter = pOutClustersCenters->getNodeColl<ClustersCenters::Coll>()[0];
const std::vector<ipReal64>& vRandomClusterElements = clusterCenter->getLeafColl<ClusterCenter::Elements>();