Skip to content

kmeans

Core and matricesfunctionOpenCV 5.0.0
import { kmeans } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

example: samples/cpp/snippets/kmeans.cpp An example on k-means clustering example: samples/python/snippets/kmeans.py An example on k-means clustering in python Finds centers of clusters and groups input samples around the clusters.

The function kmeans implements a k-means algorithm that finds the centers of cluster_count clusters and groups the input samples around the clusters. As an output, \texttt{bestLabels}_i contains a 0-based cluster index for the sample stored in the i^{th} row of the samples matrix.

Note: - (Python) An example on k-means clustering can be found at opencv_source_code/samples/python/kmeans.py

kmeans(data: Mat, K: number, bestLabels: Mat, criteria: TermCriteria, attempts: number, flags: number, centers: Mat): number;
2 available overloads
kmeans(data: Mat, K: number, bestLabels: Mat, criteria: TermCriteria, attempts: number, flags: number): number;
kmeans(data: Mat, K: number, bestLabels: Mat, criteria: TermCriteria, attempts: number, flags: number, centers: Mat): number;
data

Data for clustering. An array of N-Dimensional points with float coordinates is needed. Examples of this array can be:

  • Mat points(count, 2, CV_32F);
  • Mat points(count, 1, CV_32FC2);
  • Mat points(1, count, CV_32FC2);
  • std::vector<cv::Point2f> points(sampleCount);
K

Number of clusters to split the set by.

bestLabels

Input/output value, modified by the native operation. Input/output integer array that stores the cluster indices for every sample.

criteria

The algorithm termination criteria, that is, the maximum number of iterations and/or the desired accuracy. The accuracy is specified as criteria.epsilon. As soon as each of the cluster centers moves by less than criteria.epsilon on some iteration, the algorithm stops.

attempts

Flag to specify the number of times the algorithm is executed using different initial labellings. The algorithm returns the labels that yield the best compactness (see the last function parameter).

flags

Flag that can take values of cv::KmeansFlags

centers

Output destination, filled by the native operation. Output matrix of the cluster centers, one row per each cluster center.

Returns

The function returns the compactness measure that is computed as

\sum _i  \| \texttt{samples} _i -  \texttt{centers} _{ \texttt{labels} _i} \| ^2

after every attempt. The best (minimum) value is chosen and the corresponding labels and the compactness value are returned by the function. Basically, you can use only the core of the function, set the number of attempts to 1, initialize labels each time using a custom algorithm, pass them with the ( flags = #KMEANS_USE_INITIAL_LABELS ) flag, and then choose the best (most-compact) clustering.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.