CLAHE Python bindings
[profile/ivi/opencv.git] / modules / core / doc / clustering.rst
1 Clustering
2 ==========
3
4 .. highlight:: cpp
5
6 kmeans
7 ------
8 Finds centers of clusters and groups input samples around the clusters.
9
10 .. ocv:function:: double kmeans( InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray() )
11
12 .. ocv:pyfunction:: cv2.kmeans(data, K, criteria, attempts, flags[, bestLabels[, centers]]) -> retval, bestLabels, centers
13
14 .. ocv:cfunction:: int cvKMeans2( const CvArr* samples, int cluster_count, CvArr* labels, CvTermCriteria termcrit, int attempts=1, CvRNG* rng=0, int flags=0, CvArr* _centers=0, double* compactness=0 )
15
16 .. ocv:pyoldfunction:: cv.KMeans2(samples, nclusters, labels, termcrit, attempts=1, flags=0, centers=None) -> float
17
18     :param samples: Floating-point matrix of input samples, one row per sample.
19
20     :param cluster_count: Number of clusters to split the set by.
21
22     :param labels: Input/output integer array that stores the cluster indices for every sample.
23
24     :param 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.
25
26     :param 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).
27
28     :param rng: CvRNG state initialized by RNG().
29
30     :param flags: Flag that can take the following values:
31
32             * **KMEANS_RANDOM_CENTERS** Select random initial centers in each attempt.
33
34             * **KMEANS_PP_CENTERS** Use ``kmeans++`` center initialization by Arthur and Vassilvitskii [Arthur2007].
35
36             * **KMEANS_USE_INITIAL_LABELS** During the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random or semi-random centers. Use one of  ``KMEANS_*_CENTERS``  flag to specify the exact method.
37
38     :param centers: Output matrix of the cluster centers, one row per each cluster center.
39
40     :param compactness: The returned value that is described below.
41
42 The function ``kmeans`` implements a k-means algorithm that finds the
43 centers of ``cluster_count`` clusters and groups the input samples
44 around the clusters. As an output,
45 :math:`\texttt{labels}_i` contains a 0-based cluster index for
46 the sample stored in the
47 :math:`i^{th}` row of the ``samples`` matrix.
48
49 The function returns the compactness measure that is computed as
50
51 .. math::
52
53     \sum _i  \| \texttt{samples} _i -  \texttt{centers} _{ \texttt{labels} _i} \| ^2
54
55 after every attempt. The best (minimum) value is chosen and the
56 corresponding labels and the compactness value are returned by the function.
57 Basically, you can use only the core of the function, set the number of
58 attempts to 1, initialize labels each time using a custom algorithm, pass them with the
59 ( ``flags`` = ``KMEANS_USE_INITIAL_LABELS`` ) flag, and then choose the best (most-compact) clustering.
60
61 partition
62 -------------
63 Splits an element set into equivalency classes.
64
65 .. ocv:function:: template<typename _Tp, class _EqPredicate> int partition( const vector<_Tp>& vec, vector<int>& labels, _EqPredicate predicate=_EqPredicate())
66
67     :param vec: Set of elements stored as a vector.
68
69     :param labels: Output vector of labels. It contains as many elements as  ``vec``. Each label  ``labels[i]``  is a 0-based cluster index of  ``vec[i]`` .   
70     
71     :param predicate: Equivalence predicate (pointer to a boolean function of two arguments or an instance of the class that has the method  ``bool operator()(const _Tp& a, const _Tp& b)`` ). The predicate returns ``true`` when the elements are certainly in the same class, and returns ``false`` if they may or may not be in the same class.
72
73 The generic function ``partition`` implements an
74 :math:`O(N^2)` algorithm for
75 splitting a set of
76 :math:`N` elements into one or more equivalency classes, as described in
77 http://en.wikipedia.org/wiki/Disjoint-set_data_structure
78 . The function
79 returns the number of equivalency classes.
80
81 .. [Arthur2007] Arthur and S. Vassilvitskii. k-means++: the advantages of careful seeding, Proceedings of the eighteenth annual ACM-SIAM symposium on Discrete algorithms, 2007