Skip to content

ANNIndex

Features and matchingclassOpenCV 5.0.0
import { ANNIndex } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

Native object: release it with using or delete(). Factories can return null; check before calling methods.


Approximate Nearest Neighbors * ***************************************************************************************

Constructors and members

static create

Creates an instance of annoy index class with given parameters

create(dim: number, distType: number): ANNIndex | null;
2 available overloads
create(dim: number): ANNIndex | null;
create(dim: number, distType: number): ANNIndex | null;
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.

Returns

The ANNIndex | null result.

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

addItems

Add feature vectors to index.

addItems(features: Mat): void;
features

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

build

Build the index.

build(trees: number): void;
2 available overloads
build(): void;
build(trees: number): void;
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.

knnSearch

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

knnSearch(query: Mat, indices: Mat, dists: Mat, knn: number, search_k: number): void;
2 available overloads
knnSearch(query: Mat, indices: Mat, dists: Mat, knn: number): void;
knnSearch(query: Mat, indices: Mat, dists: Mat, knn: number, search_k: number): void;
query

The query vector(s).

indices

Output destination, filled by the native operation. Matrix that will contain the indices of the K-nearest neighbors found, optional.

dists

Output destination, filled by the native operation. 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.

save

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

save(filename: EmbindString, prefault: boolean): void;
2 available overloads
save(filename: EmbindString): void;
save(filename: EmbindString, prefault: boolean): void;
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.

load

Loads (mmaps) an index from disk.

load(filename: EmbindString, prefault: boolean): void;
2 available overloads
load(filename: EmbindString): void;
load(filename: EmbindString, prefault: boolean): void;
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.

getTreeNumber

Return the number of trees in the index.

getTreeNumber(): number;
Returns

The number result.

getItemNumber

Return the number of feature vectors in the index.

getItemNumber(): number;
Returns

The number result.

setOnDiskBuild

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

setOnDiskBuild(filename: EmbindString): boolean;
filename

Filename of the index to be built.

Returns

The boolean result.

setSeed

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().

setSeed(seed: number): void;
seed

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

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.