Approximate Nearest Neighbors Search in Multi-Dimensional Spaces#

This section documents OpenCV’s interface to the Annoy. Annoy (Approximate Nearest Neighbors Oh Yeah) is a library to search for points in space that are close to a given query point. It also creates large read-only file-based data structures that are mmapped into memory so that many processes may share the same data.

Classes#

Name

Description

class cv::ANNIndex

Class cv::ANNIndex#

Collaboration diagram for cv::ANNIndex:

Public Types#

Metrics used to calculate the distance between two feature vectors.

Member Enumeration Documentation#

enum Distance

Metrics used to calculate the distance between two feature vectors.

DIST_EUCLIDEAN
Python: cv.ANNIndex_DIST_EUCLIDEAN

DIST_MANHATTAN
Python: cv.ANNIndex_DIST_MANHATTAN

DIST_ANGULAR
Python: cv.ANNIndex_DIST_ANGULAR

DIST_HAMMING
Python: cv.ANNIndex_DIST_HAMMING

DIST_DOTPRODUCT
Python: cv.ANNIndex_DIST_DOTPRODUCT

Constructor & Destructor Documentation#

~ANNIndex()#

cv::ANNIndex::~ANNIndex()

Member Function Documentation#

addItems()#

void cv::ANNIndex::addItems(InputArray features)

Python:

cv.ANNIndex.addItems(features)

Add feature vectors to index.

Parameters

  • features — Matrix containing the feature vectors to index. The size of the matrix is num_features x feature_dimension.

build()#

void cv::ANNIndex::build(int trees = -1)

Python:

cv.ANNIndex.build([, trees])

Build the index.

Parameters

  • trees — Number of trees in the index. If not provided, the number is determined automatically in a way that at most 2x as much memory as the features vectors take is used.

getItemNumber()#

int cv::ANNIndex::getItemNumber()

Python:

cv.ANNIndex.getItemNumber() -> retval

Return the number of feature vectors in the index.

getTreeNumber()#

int cv::ANNIndex::getTreeNumber()

Python:

cv.ANNIndex.getTreeNumber() -> retval

Return the number of trees in the index.

knnSearch()#

void cv::ANNIndex::knnSearch(
InputArray query,
OutputArray indices,
OutputArray dists,
int knn,
int search_k = -1 )

Python:

cv.ANNIndex.knnSearch(query, knn[, indices[, dists[, search_k]]]) -> indices, dists

Performs a K-nearest neighbor search for given query vector(s) using the index.

Parameters

  • query — The query vector(s).

  • indices — Matrix that will contain the indices of the K-nearest neighbors found, optional.

  • dists — Matrix that will contain the distances to the K-nearest neighbors found, optional.

  • knn — Number of nearest neighbors to search for.

  • search_k — The maximum number of nodes to inspect, which defaults to trees x knn if not provided.

load()#

void cv::ANNIndex::load(
const String & filename,
bool prefault = false )

Python:

cv.ANNIndex.load(filename[, prefault])

Loads (mmaps) an index from disk.

Parameters

  • filename — Filename of the index to be loaded.

  • prefault — If prefault is set to true, it will pre-read the entire file into memory (using mmap with MAP_POPULATE). Default is false.

save()#

void cv::ANNIndex::save(
const String & filename,
bool prefault = false )

Python:

cv.ANNIndex.save(filename[, prefault])

Save the index to disk and loads it. After saving, no more vectors can be added.

Parameters

  • filename — Filename of the index to be saved.

  • prefault — If prefault is set to true, it will pre-read the entire file into memory (using mmap with MAP_POPULATE). Default is false.

setOnDiskBuild()#

bool cv::ANNIndex::setOnDiskBuild(const String & filename)

Python:

cv.ANNIndex.setOnDiskBuild(filename) -> retval

Prepare to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)

Parameters

  • filename — Filename of the index to be built.

setSeed()#

void cv::ANNIndex::setSeed(int seed)

Python:

cv.ANNIndex.setSeed(seed)

Initialize the random number generator with the given seed. Only necessary to pass this before adding the items. Will have no effect after calling build() or load().

Parameters

  • seed — The given seed of the random number generator. Its value should be within the range of uint32_t.

create()#

static Ptr< ANNIndex > cv::ANNIndex::create(
int dim,
ANNIndex::Distance distType = ANNIndex::DIST_EUCLIDEAN )

Python:

cv.ANNIndex.create(dim[, distType]) -> retval
cv.ANNIndex_create(dim[, distType]) -> retval

Creates an instance of annoy index class with given parameters.

Parameters

  • dim — The dimension of the feature vector.

  • distType — Metric to calculate the distance between two feature vectors, can be DIST_EUCLIDEAN, DIST_MANHATTAN, DIST_ANGULAR, DIST_HAMMING, or DIST_DOTPRODUCT.

Source file#

The documentation for this class was generated from the following file: