Class ANNIndex

java.lang.Object
org.opencv.features.ANNIndex

public class ANNIndex extends Object
*************************************************************************************\ Approximate Nearest Neighbors * \***************************************************************************************
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    protected final long
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    ANNIndex(long addr)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static ANNIndex
    __fromPtr__(long addr)
     
    void
    addItems(Mat features)
    Add feature vectors to index.
    void
    Build the index.
    void
    build(int trees)
    Build the index.
    static ANNIndex
    create(int dim)
    Creates an instance of annoy index class with given parameters
    static ANNIndex
    create(int dim, int distType)
    Creates an instance of annoy index class with given parameters
    int
    Return the number of feature vectors in the index.
    long
     
    int
    Return the number of trees in the index.
    void
    knnSearch(Mat query, Mat indices, Mat dists, int knn)
    Performs a K-nearest neighbor search for given query vector(s) using the index.
    void
    knnSearch(Mat query, Mat indices, Mat dists, int knn, int search_k)
    Performs a K-nearest neighbor search for given query vector(s) using the index.
    void
    load(String filename)
    Loads (mmaps) an index from disk.
    void
    load(String filename, boolean prefault)
    Loads (mmaps) an index from disk.
    void
    save(String filename)
    Save the index to disk and loads it.
    void
    save(String filename, boolean prefault)
    Save the index to disk and loads it.
    boolean
    Prepare to build the index in the specified file instead of RAM (execute before adding items, no need to save after build)
    void
    setSeed(int seed)
    Initialize the random number generator with the given seed.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • ANNIndex

      protected ANNIndex(long addr)
  • Method Details

    • getNativeObjAddr

      public long getNativeObjAddr()
    • __fromPtr__

      public static ANNIndex __fromPtr__(long addr)
    • addItems

      public void addItems(Mat 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

      public void build(int 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.
    • build

      public void build()
      Build the index. in a way that at most 2x as much memory as the features vectors take is used.
    • knnSearch

      public void knnSearch(Mat query, Mat indices, Mat dists, int knn, int search_k)
      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.
    • knnSearch

      public void knnSearch(Mat query, Mat indices, Mat dists, int knn)
      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.
    • save

      public void save(String filename, boolean 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.
    • save

      public void save(String filename)
      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. with MAP_POPULATE). Default is false.
    • load

      public void load(String filename, boolean 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.
    • load

      public void load(String filename)
      Loads (mmaps) an index from disk.
      Parameters:
      filename - Filename of the index to be loaded. with MAP_POPULATE). Default is false.
    • getTreeNumber

      public int getTreeNumber()
      Return the number of trees in the index.
      Returns:
      automatically generated
    • getItemNumber

      public int getItemNumber()
      Return the number of feature vectors in the index.
      Returns:
      automatically generated
    • setOnDiskBuild

      public boolean setOnDiskBuild(String filename)
      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.
      Returns:
      automatically generated
    • setSeed

      public void setSeed(int 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

      public static ANNIndex create(int dim, int distType)
      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.
      Returns:
      automatically generated
    • create

      public static ANNIndex create(int dim)
      Creates an instance of annoy index class with given parameters
      Parameters:
      dim - The dimension of the feature vector. DIST_MANHATTAN, DIST_ANGULAR, DIST_HAMMING, or DIST_DOTPRODUCT.
      Returns:
      automatically generated