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_NNINDEX_H
RyoheiHagimoto 0:0e0631af0305 32 #define OPENCV_FLANN_NNINDEX_H
RyoheiHagimoto 0:0e0631af0305 33
RyoheiHagimoto 0:0e0631af0305 34 #include "general.h"
RyoheiHagimoto 0:0e0631af0305 35 #include "matrix.h"
RyoheiHagimoto 0:0e0631af0305 36 #include "result_set.h"
RyoheiHagimoto 0:0e0631af0305 37 #include "params.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 * Nearest-neighbour index base class
RyoheiHagimoto 0:0e0631af0305 44 */
RyoheiHagimoto 0:0e0631af0305 45 template <typename Distance>
RyoheiHagimoto 0:0e0631af0305 46 class NNIndex
RyoheiHagimoto 0:0e0631af0305 47 {
RyoheiHagimoto 0:0e0631af0305 48 typedef typename Distance::ElementType ElementType;
RyoheiHagimoto 0:0e0631af0305 49 typedef typename Distance::ResultType DistanceType;
RyoheiHagimoto 0:0e0631af0305 50
RyoheiHagimoto 0:0e0631af0305 51 public:
RyoheiHagimoto 0:0e0631af0305 52
RyoheiHagimoto 0:0e0631af0305 53 virtual ~NNIndex() {}
RyoheiHagimoto 0:0e0631af0305 54
RyoheiHagimoto 0:0e0631af0305 55 /**
RyoheiHagimoto 0:0e0631af0305 56 * \brief Builds the index
RyoheiHagimoto 0:0e0631af0305 57 */
RyoheiHagimoto 0:0e0631af0305 58 virtual void buildIndex() = 0;
RyoheiHagimoto 0:0e0631af0305 59
RyoheiHagimoto 0:0e0631af0305 60 /**
RyoheiHagimoto 0:0e0631af0305 61 * \brief Perform k-nearest neighbor search
RyoheiHagimoto 0:0e0631af0305 62 * \param[in] queries The query points for which to find the nearest neighbors
RyoheiHagimoto 0:0e0631af0305 63 * \param[out] indices The indices of the nearest neighbors found
RyoheiHagimoto 0:0e0631af0305 64 * \param[out] dists Distances to the nearest neighbors found
RyoheiHagimoto 0:0e0631af0305 65 * \param[in] knn Number of nearest neighbors to return
RyoheiHagimoto 0:0e0631af0305 66 * \param[in] params Search parameters
RyoheiHagimoto 0:0e0631af0305 67 */
RyoheiHagimoto 0:0e0631af0305 68 virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
RyoheiHagimoto 0:0e0631af0305 69 {
RyoheiHagimoto 0:0e0631af0305 70 assert(queries.cols == veclen());
RyoheiHagimoto 0:0e0631af0305 71 assert(indices.rows >= queries.rows);
RyoheiHagimoto 0:0e0631af0305 72 assert(dists.rows >= queries.rows);
RyoheiHagimoto 0:0e0631af0305 73 assert(int(indices.cols) >= knn);
RyoheiHagimoto 0:0e0631af0305 74 assert(int(dists.cols) >= knn);
RyoheiHagimoto 0:0e0631af0305 75
RyoheiHagimoto 0:0e0631af0305 76 #if 0
RyoheiHagimoto 0:0e0631af0305 77 KNNResultSet<DistanceType> resultSet(knn);
RyoheiHagimoto 0:0e0631af0305 78 for (size_t i = 0; i < queries.rows; i++) {
RyoheiHagimoto 0:0e0631af0305 79 resultSet.init(indices[i], dists[i]);
RyoheiHagimoto 0:0e0631af0305 80 findNeighbors(resultSet, queries[i], params);
RyoheiHagimoto 0:0e0631af0305 81 }
RyoheiHagimoto 0:0e0631af0305 82 #else
RyoheiHagimoto 0:0e0631af0305 83 KNNUniqueResultSet<DistanceType> resultSet(knn);
RyoheiHagimoto 0:0e0631af0305 84 for (size_t i = 0; i < queries.rows; i++) {
RyoheiHagimoto 0:0e0631af0305 85 resultSet.clear();
RyoheiHagimoto 0:0e0631af0305 86 findNeighbors(resultSet, queries[i], params);
RyoheiHagimoto 0:0e0631af0305 87 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
RyoheiHagimoto 0:0e0631af0305 88 else resultSet.copy(indices[i], dists[i], knn);
RyoheiHagimoto 0:0e0631af0305 89 }
RyoheiHagimoto 0:0e0631af0305 90 #endif
RyoheiHagimoto 0:0e0631af0305 91 }
RyoheiHagimoto 0:0e0631af0305 92
RyoheiHagimoto 0:0e0631af0305 93 /**
RyoheiHagimoto 0:0e0631af0305 94 * \brief Perform radius search
RyoheiHagimoto 0:0e0631af0305 95 * \param[in] query The query point
RyoheiHagimoto 0:0e0631af0305 96 * \param[out] indices The indinces of the neighbors found within the given radius
RyoheiHagimoto 0:0e0631af0305 97 * \param[out] dists The distances to the nearest neighbors found
RyoheiHagimoto 0:0e0631af0305 98 * \param[in] radius The radius used for search
RyoheiHagimoto 0:0e0631af0305 99 * \param[in] params Search parameters
RyoheiHagimoto 0:0e0631af0305 100 * \returns Number of neighbors found
RyoheiHagimoto 0:0e0631af0305 101 */
RyoheiHagimoto 0:0e0631af0305 102 virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
RyoheiHagimoto 0:0e0631af0305 103 {
RyoheiHagimoto 0:0e0631af0305 104 if (query.rows != 1) {
RyoheiHagimoto 0:0e0631af0305 105 fprintf(stderr, "I can only search one feature at a time for range search\n");
RyoheiHagimoto 0:0e0631af0305 106 return -1;
RyoheiHagimoto 0:0e0631af0305 107 }
RyoheiHagimoto 0:0e0631af0305 108 assert(query.cols == veclen());
RyoheiHagimoto 0:0e0631af0305 109 assert(indices.cols == dists.cols);
RyoheiHagimoto 0:0e0631af0305 110
RyoheiHagimoto 0:0e0631af0305 111 int n = 0;
RyoheiHagimoto 0:0e0631af0305 112 int* indices_ptr = NULL;
RyoheiHagimoto 0:0e0631af0305 113 DistanceType* dists_ptr = NULL;
RyoheiHagimoto 0:0e0631af0305 114 if (indices.cols > 0) {
RyoheiHagimoto 0:0e0631af0305 115 n = (int)indices.cols;
RyoheiHagimoto 0:0e0631af0305 116 indices_ptr = indices[0];
RyoheiHagimoto 0:0e0631af0305 117 dists_ptr = dists[0];
RyoheiHagimoto 0:0e0631af0305 118 }
RyoheiHagimoto 0:0e0631af0305 119
RyoheiHagimoto 0:0e0631af0305 120 RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius);
RyoheiHagimoto 0:0e0631af0305 121 resultSet.clear();
RyoheiHagimoto 0:0e0631af0305 122 findNeighbors(resultSet, query[0], params);
RyoheiHagimoto 0:0e0631af0305 123 if (n>0) {
RyoheiHagimoto 0:0e0631af0305 124 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n);
RyoheiHagimoto 0:0e0631af0305 125 else resultSet.copy(indices_ptr, dists_ptr, n);
RyoheiHagimoto 0:0e0631af0305 126 }
RyoheiHagimoto 0:0e0631af0305 127
RyoheiHagimoto 0:0e0631af0305 128 return (int)resultSet.size();
RyoheiHagimoto 0:0e0631af0305 129 }
RyoheiHagimoto 0:0e0631af0305 130
RyoheiHagimoto 0:0e0631af0305 131 /**
RyoheiHagimoto 0:0e0631af0305 132 * \brief Saves the index to a stream
RyoheiHagimoto 0:0e0631af0305 133 * \param stream The stream to save the index to
RyoheiHagimoto 0:0e0631af0305 134 */
RyoheiHagimoto 0:0e0631af0305 135 virtual void saveIndex(FILE* stream) = 0;
RyoheiHagimoto 0:0e0631af0305 136
RyoheiHagimoto 0:0e0631af0305 137 /**
RyoheiHagimoto 0:0e0631af0305 138 * \brief Loads the index from a stream
RyoheiHagimoto 0:0e0631af0305 139 * \param stream The stream from which the index is loaded
RyoheiHagimoto 0:0e0631af0305 140 */
RyoheiHagimoto 0:0e0631af0305 141 virtual void loadIndex(FILE* stream) = 0;
RyoheiHagimoto 0:0e0631af0305 142
RyoheiHagimoto 0:0e0631af0305 143 /**
RyoheiHagimoto 0:0e0631af0305 144 * \returns number of features in this index.
RyoheiHagimoto 0:0e0631af0305 145 */
RyoheiHagimoto 0:0e0631af0305 146 virtual size_t size() const = 0;
RyoheiHagimoto 0:0e0631af0305 147
RyoheiHagimoto 0:0e0631af0305 148 /**
RyoheiHagimoto 0:0e0631af0305 149 * \returns The dimensionality of the features in this index.
RyoheiHagimoto 0:0e0631af0305 150 */
RyoheiHagimoto 0:0e0631af0305 151 virtual size_t veclen() const = 0;
RyoheiHagimoto 0:0e0631af0305 152
RyoheiHagimoto 0:0e0631af0305 153 /**
RyoheiHagimoto 0:0e0631af0305 154 * \returns The amount of memory (in bytes) used by the index.
RyoheiHagimoto 0:0e0631af0305 155 */
RyoheiHagimoto 0:0e0631af0305 156 virtual int usedMemory() const = 0;
RyoheiHagimoto 0:0e0631af0305 157
RyoheiHagimoto 0:0e0631af0305 158 /**
RyoheiHagimoto 0:0e0631af0305 159 * \returns The index type (kdtree, kmeans,...)
RyoheiHagimoto 0:0e0631af0305 160 */
RyoheiHagimoto 0:0e0631af0305 161 virtual flann_algorithm_t getType() const = 0;
RyoheiHagimoto 0:0e0631af0305 162
RyoheiHagimoto 0:0e0631af0305 163 /**
RyoheiHagimoto 0:0e0631af0305 164 * \returns The index parameters
RyoheiHagimoto 0:0e0631af0305 165 */
RyoheiHagimoto 0:0e0631af0305 166 virtual IndexParams getParameters() const = 0;
RyoheiHagimoto 0:0e0631af0305 167
RyoheiHagimoto 0:0e0631af0305 168
RyoheiHagimoto 0:0e0631af0305 169 /**
RyoheiHagimoto 0:0e0631af0305 170 * \brief Method that searches for nearest-neighbours
RyoheiHagimoto 0:0e0631af0305 171 */
RyoheiHagimoto 0:0e0631af0305 172 virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0;
RyoheiHagimoto 0:0e0631af0305 173 };
RyoheiHagimoto 0:0e0631af0305 174
RyoheiHagimoto 0:0e0631af0305 175 }
RyoheiHagimoto 0:0e0631af0305 176
RyoheiHagimoto 0:0e0631af0305 177 #endif //OPENCV_FLANN_NNINDEX_H