Table of Contents

Class ANNIndex

Namespace
OpenCvSharp
Assembly
OpenCvSharp.dll

Approximate nearest neighbor index backed by Annoy (OpenCV 5), a modern alternative to FLANN.

public class ANNIndex : CvPtrObject, IDisposable
Inheritance
ANNIndex
Implements
Inherited Members

Methods

AddItems(InputArray)

Adds feature vectors to the index.

public void AddItems(InputArray features)

Parameters

features InputArray

Matrix containing the feature vectors to index (num_features x feature_dimension).

Build(int)

Builds the index.

public void Build(int trees = -1)

Parameters

trees int

Number of trees in the index. -1 determines the number automatically.

Create(int, ANNIndexDistance)

Creates an instance of the Annoy index with the given parameters.

public static ANNIndex Create(int dim, ANNIndexDistance distType = ANNIndexDistance.Euclidean)

Parameters

dim int

The dimension of the feature vector.

distType ANNIndexDistance

Metric used to calculate the distance between two feature vectors.

Returns

ANNIndex

GetItemNumber()

Returns the number of feature vectors in the index.

public int GetItemNumber()

Returns

int

GetTreeNumber()

Returns the number of trees in the index.

public int GetTreeNumber()

Returns

int

KnnSearch(InputArray, OutputArray, OutputArray, int, int)

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

public void KnnSearch(InputArray query, OutputArray indices, OutputArray dists, int knn, int searchK = -1)

Parameters

query InputArray

The query vector(s).

indices OutputArray

Output matrix that will contain the indices of the K-nearest neighbors found.

dists OutputArray

Output matrix that will contain the distances to the K-nearest neighbors found.

knn int

Number of nearest neighbors to search for.

searchK int

The maximum number of nodes to inspect; defaults to trees x knn when -1.

Load(string, bool)

Loads (mmaps) an index from disk.

public void Load(string filename, bool prefault = false)

Parameters

filename string

Filename of the index to be loaded.

prefault bool

If true, pre-reads the entire file into memory.

Save(string, bool)

Saves the index to disk. After saving, no more vectors can be added.

public void Save(string filename, bool prefault = false)

Parameters

filename string

Filename of the index to be saved.

prefault bool

If true, pre-reads the entire file into memory.

SetOnDiskBuild(string)

Prepares to build the index in the specified file instead of in RAM (call before adding items; no need to save after build).

public bool SetOnDiskBuild(string filename)

Parameters

filename string

Filename of the index to be built.

Returns

bool

True on success.

SetSeed(int)

Initializes the random number generator with the given seed. Only effective before adding items / calling build() or load().

public void SetSeed(int seed)

Parameters

seed int

The seed of the random number generator.