openCV library for Renesas RZ/A

Dependents:   RZ_A2M_Mbed_samples

Committer:
RyoheiHagimoto
Date:
Fri Jan 29 04:53:38 2021 +0000
Revision:
0:0e0631af0305
copied from https://github.com/d-kato/opencv-lib.

Who changed what in which revision?

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