Click or drag to resize

Cv2Kmeans Method

Finds centers of clusters and groups input samples around the clusters.

Namespace:  OpenCvSharp
Assembly:  OpenCvSharp (in OpenCvSharp.dll) Version: 1.0.0
Syntax
public static double Kmeans(
	InputArray data,
	int k,
	InputOutputArray bestLabels,
	TermCriteria criteria,
	int attempts,
	KMeansFlags flags,
	OutputArray centers = null
)

Parameters

data
Type: OpenCvSharpInputArray
Data for clustering. An array of N-Dimensional points with float coordinates is needed.
k
Type: SystemInt32
Number of clusters to split the set by.
bestLabels
Type: OpenCvSharpInputOutputArray
Input/output integer array that stores the cluster indices for every sample.
criteria
Type: OpenCvSharpTermCriteria
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
Type: SystemInt32
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
Type: OpenCvSharpKMeansFlags
Flag that can take values of cv::KmeansFlags
centers (Optional)
Type: OpenCvSharpOutputArray
Output matrix of the cluster centers, one row per each cluster center.

Return Value

Type: Double
The function returns the compactness measure that is computed as \f[\sum _i \| \texttt{samples} _i - \texttt{centers} _{ \texttt{labels} _i} \| ^2\f] 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.
See Also