github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/flann/composite_index.h (about)

     1  /***********************************************************************
     2   * Software License Agreement (BSD License)
     3   *
     4   * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
     5   * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
     6   *
     7   * THE BSD LICENSE
     8   *
     9   * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   * 2. Redistributions in binary form must reproduce the above copyright
    16   *    notice, this list of conditions and the following disclaimer in the
    17   *    documentation and/or other materials provided with the distribution.
    18   *
    19   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    20   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    21   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    22   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    23   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    24   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    28   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29   *************************************************************************/
    30  
    31  #ifndef OPENCV_FLANN_COMPOSITE_INDEX_H_
    32  #define OPENCV_FLANN_COMPOSITE_INDEX_H_
    33  
    34  //! @cond IGNORED
    35  
    36  #include "nn_index.h"
    37  #include "kdtree_index.h"
    38  #include "kmeans_index.h"
    39  
    40  namespace cvflann
    41  {
    42  
    43  /**
    44   * Index parameters for the CompositeIndex.
    45   */
    46  struct CompositeIndexParams : public IndexParams
    47  {
    48      CompositeIndexParams(int trees = 4, int branching = 32, int iterations = 11,
    49                           flann_centers_init_t centers_init = FLANN_CENTERS_RANDOM, float cb_index = 0.2 )
    50      {
    51          (*this)["algorithm"] = FLANN_INDEX_KMEANS;
    52          // number of randomized trees to use (for kdtree)
    53          (*this)["trees"] = trees;
    54          // branching factor
    55          (*this)["branching"] = branching;
    56          // max iterations to perform in one kmeans clustering (kmeans tree)
    57          (*this)["iterations"] = iterations;
    58          // algorithm used for picking the initial cluster centers for kmeans tree
    59          (*this)["centers_init"] = centers_init;
    60          // cluster boundary index. Used when searching the kmeans tree
    61          (*this)["cb_index"] = cb_index;
    62      }
    63  };
    64  
    65  
    66  /**
    67   * This index builds a kd-tree index and a k-means index and performs nearest
    68   * neighbour search both indexes. This gives a slight boost in search performance
    69   * as some of the neighbours that are missed by one index are found by the other.
    70   */
    71  template <typename Distance>
    72  class CompositeIndex : public NNIndex<Distance>
    73  {
    74  public:
    75      typedef typename Distance::ElementType ElementType;
    76      typedef typename Distance::ResultType DistanceType;
    77  
    78      /**
    79       * Index constructor
    80       * @param inputData dataset containing the points to index
    81       * @param params Index parameters
    82       * @param d Distance functor
    83       * @return
    84       */
    85      CompositeIndex(const Matrix<ElementType>& inputData, const IndexParams& params = CompositeIndexParams(),
    86                     Distance d = Distance()) : index_params_(params)
    87      {
    88          kdtree_index_ = new KDTreeIndex<Distance>(inputData, params, d);
    89          kmeans_index_ = new KMeansIndex<Distance>(inputData, params, d);
    90  
    91      }
    92  
    93      CompositeIndex(const CompositeIndex&);
    94      CompositeIndex& operator=(const CompositeIndex&);
    95  
    96      virtual ~CompositeIndex()
    97      {
    98          delete kdtree_index_;
    99          delete kmeans_index_;
   100      }
   101  
   102      /**
   103       * @return The index type
   104       */
   105      flann_algorithm_t getType() const CV_OVERRIDE
   106      {
   107          return FLANN_INDEX_COMPOSITE;
   108      }
   109  
   110      /**
   111       * @return Size of the index
   112       */
   113      size_t size() const CV_OVERRIDE
   114      {
   115          return kdtree_index_->size();
   116      }
   117  
   118      /**
   119       * \returns The dimensionality of the features in this index.
   120       */
   121      size_t veclen() const CV_OVERRIDE
   122      {
   123          return kdtree_index_->veclen();
   124      }
   125  
   126      /**
   127       * \returns The amount of memory (in bytes) used by the index.
   128       */
   129      int usedMemory() const CV_OVERRIDE
   130      {
   131          return kmeans_index_->usedMemory() + kdtree_index_->usedMemory();
   132      }
   133  
   134      /**
   135       * \brief Builds the index
   136       */
   137      void buildIndex() CV_OVERRIDE
   138      {
   139          Logger::info("Building kmeans tree...\n");
   140          kmeans_index_->buildIndex();
   141          Logger::info("Building kdtree tree...\n");
   142          kdtree_index_->buildIndex();
   143      }
   144  
   145      /**
   146       * \brief Saves the index to a stream
   147       * \param stream The stream to save the index to
   148       */
   149      void saveIndex(FILE* stream) CV_OVERRIDE
   150      {
   151          kmeans_index_->saveIndex(stream);
   152          kdtree_index_->saveIndex(stream);
   153      }
   154  
   155      /**
   156       * \brief Loads the index from a stream
   157       * \param stream The stream from which the index is loaded
   158       */
   159      void loadIndex(FILE* stream) CV_OVERRIDE
   160      {
   161          kmeans_index_->loadIndex(stream);
   162          kdtree_index_->loadIndex(stream);
   163      }
   164  
   165      /**
   166       * \returns The index parameters
   167       */
   168      IndexParams getParameters() const CV_OVERRIDE
   169      {
   170          return index_params_;
   171      }
   172  
   173      /**
   174       * \brief Method that searches for nearest-neighbours
   175       */
   176      void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) CV_OVERRIDE
   177      {
   178          kmeans_index_->findNeighbors(result, vec, searchParams);
   179          kdtree_index_->findNeighbors(result, vec, searchParams);
   180      }
   181  
   182  private:
   183      /** The k-means index */
   184      KMeansIndex<Distance>* kmeans_index_;
   185  
   186      /** The kd-tree index */
   187      KDTreeIndex<Distance>* kdtree_index_;
   188  
   189      /** The index parameters */
   190      const IndexParams index_params_;
   191  };
   192  
   193  }
   194  
   195  //! @endcond
   196  
   197  #endif //OPENCV_FLANN_COMPOSITE_INDEX_H_