template class cvflann::KMeansIndex

Overview

Hierarchical kmeans index

Contains a tree constructed through a hierarchical kmeans clustering and other information for indexing a set of points for nearest-neighbour matching. More…

#include <kmeans_index.h>

template <typename Distance>
class KMeansIndex: public cvflann::NNIndex
{
public:
    // typedefs

    typedef void(KMeansIndex::* centersAlgFunction)(
        int,
        int *,
        int,
        int *,
        int &
        );

    typedef Distance::ResultType DistanceType;
    typedef Distance::ElementType ElementType;

    // structs

    struct KMeansNode;

    // classes

    class KMeansDistanceComputer;

    // fields

    centersAlgFunction chooseCenters;

    // construction

    KMeansIndex(
        const Matrix<ElementType>& inputData,
        const IndexParams& params = KMeansIndexParams(),
        Distance d = Distance()
        );

    KMeansIndex(const KMeansIndex&);

    // methods

    virtual
    void
    buildIndex();

    void
    chooseCentersGonzales(
        int k,
        int* indices,
        int indices_length,
        int* centers,
        int& centers_length
        );

    void
    chooseCentersKMeanspp(
        int k,
        int* indices,
        int indices_length,
        int* centers,
        int& centers_length
        );

    void
    chooseCentersRandom(
        int k,
        int* indices,
        int indices_length,
        int* centers,
        int& centers_length
        );

    virtual
    void
    findNeighbors(
        ResultSet<DistanceType>& result,
        const ElementType* vec,
        const SearchParams& searchParams
        );

    int
    getClusterCenters(Matrix<DistanceType>& centers);

    virtual
    IndexParams
    getParameters() const;

    virtual
    flann_algorithm_t
    getType() const;

    virtual
    void
    loadIndex(FILE* stream);

    KMeansIndex&
    operator=(const KMeansIndex&);

    virtual
    void
    saveIndex(FILE* stream);

    void
    set_cb_index(float index);

    virtual
    size_t
    size() const;

    virtual
    int
    usedMemory() const;

    virtual
    size_t
    veclen() const;
};

Inherited Members

public:
    // methods

    virtual
    void
    buildIndex() = 0;

    virtual
    void
    findNeighbors(
        ResultSet<DistanceType>& result,
        const ElementType* vec,
        const SearchParams& searchParams
        ) = 0;

    virtual
    IndexParams
    getParameters() const = 0;

    virtual
    flann_algorithm_t
    getType() const = 0;

    virtual
    void
    knnSearch(
        const Matrix<ElementType>& queries,
        Matrix<int>& indices,
        Matrix<DistanceType>& dists,
        int knn,
        const SearchParams& params
        );

    virtual
    void
    loadIndex(FILE* stream) = 0;

    virtual
    int
    radiusSearch(
        const Matrix<ElementType>& query,
        Matrix<int>& indices,
        Matrix<DistanceType>& dists,
        float radius,
        const SearchParams& params
        );

    virtual
    void
    saveIndex(FILE* stream) = 0;

    virtual
    size_t
    size() const = 0;

    virtual
    int
    usedMemory() const = 0;

    virtual
    size_t
    veclen() const = 0;

Detailed Documentation

Hierarchical kmeans index

Contains a tree constructed through a hierarchical kmeans clustering and other information for indexing a set of points for nearest-neighbour matching.

Fields

centersAlgFunction chooseCenters

The function used for choosing the cluster centers.

Construction

KMeansIndex(
    const Matrix<ElementType>& inputData,
    const IndexParams& params = KMeansIndexParams(),
    Distance d = Distance()
    )

Index constructor

Params: inputData = dataset with the input features params = parameters passed to the hierarchical k-means algorithm

Methods

virtual
void
buildIndex()

Builds the index

void
chooseCentersGonzales(
    int k,
    int* indices,
    int indices_length,
    int* centers,
    int& centers_length
    )

Chooses the initial centers in the k-means using Gonzales’ algorithm so that the centers are spaced apart from each other.

Params: k = number of centers vecs = the dataset of points indices = indices in the dataset Returns:

void
chooseCentersKMeanspp(
    int k,
    int* indices,
    int indices_length,
    int* centers,
    int& centers_length
    )

Chooses the initial centers in the k-means using the algorithm proposed in the KMeans++ paper: Arthur, David; Vassilvitskii, Sergei - k-means++: The Advantages of Careful Seeding

Implementation of this function was converted from the one provided in Arthur’s code.

Params: k = number of centers vecs = the dataset of points indices = indices in the dataset Returns:

void
chooseCentersRandom(
    int k,
    int* indices,
    int indices_length,
    int* centers,
    int& centers_length
    )

Chooses the initial centers in the k-means clustering in a random manner.

Params: k = number of centers vecs = the dataset of points indices = indices in the dataset indices_length = length of indices vector

virtual
void
findNeighbors(
    ResultSet<DistanceType>& result,
    const ElementType* vec,
    const SearchParams& searchParams
    )

Find set of nearest neighbors to vec. Their indices are stored inside the result object.

Params: result = the result object in which the indices of the nearest-neighbors are stored vec = the vector for which to search the nearest neighbors searchParams = parameters that influence the search algorithm (checks, cb_index)

int
getClusterCenters(Matrix<DistanceType>& centers)

Clustering function that takes a cut in the hierarchical k-means tree and return the clusters centers of that clustering. Params: numClusters = number of clusters to have in the clustering computed Returns: number of cluster centers

virtual
IndexParams
getParameters() const

Returns:

The index parameters

virtual
flann_algorithm_t
getType() const

Returns:

The index type (kdtree, kmeans,…)

virtual
void
loadIndex(FILE* stream)

Loads the index from a stream.

Parameters:

stream The stream from which the index is loaded
virtual
void
saveIndex(FILE* stream)

Saves the index to a stream.

Parameters:

stream The stream to save the index to
virtual
size_t
size() const

Returns size of index.

virtual
int
usedMemory() const

Computes the inde memory usage Returns: memory used by the index

virtual
size_t
veclen() const

Returns the length of an index feature.