opencv on mbed

Dependencies:   mbed

Committer:
joeverbout
Date:
Thu Mar 31 21:16:38 2016 +0000
Revision:
0:ea44dc9ed014
OpenCV on mbed attempt

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeverbout 0:ea44dc9ed014 1 /***********************************************************************
joeverbout 0:ea44dc9ed014 2 * Software License Agreement (BSD License)
joeverbout 0:ea44dc9ed014 3 *
joeverbout 0:ea44dc9ed014 4 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
joeverbout 0:ea44dc9ed014 5 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
joeverbout 0:ea44dc9ed014 6 *
joeverbout 0:ea44dc9ed014 7 * THE BSD LICENSE
joeverbout 0:ea44dc9ed014 8 *
joeverbout 0:ea44dc9ed014 9 * Redistribution and use in source and binary forms, with or without
joeverbout 0:ea44dc9ed014 10 * modification, are permitted provided that the following conditions
joeverbout 0:ea44dc9ed014 11 * are met:
joeverbout 0:ea44dc9ed014 12 *
joeverbout 0:ea44dc9ed014 13 * 1. Redistributions of source code must retain the above copyright
joeverbout 0:ea44dc9ed014 14 * notice, this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 15 * 2. Redistributions in binary form must reproduce the above copyright
joeverbout 0:ea44dc9ed014 16 * notice, this list of conditions and the following disclaimer in the
joeverbout 0:ea44dc9ed014 17 * documentation and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 18 *
joeverbout 0:ea44dc9ed014 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
joeverbout 0:ea44dc9ed014 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
joeverbout 0:ea44dc9ed014 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
joeverbout 0:ea44dc9ed014 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
joeverbout 0:ea44dc9ed014 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
joeverbout 0:ea44dc9ed014 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
joeverbout 0:ea44dc9ed014 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
joeverbout 0:ea44dc9ed014 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
joeverbout 0:ea44dc9ed014 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
joeverbout 0:ea44dc9ed014 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
joeverbout 0:ea44dc9ed014 29 *************************************************************************/
joeverbout 0:ea44dc9ed014 30
joeverbout 0:ea44dc9ed014 31 #ifndef OPENCV_FLANN_NNINDEX_H
joeverbout 0:ea44dc9ed014 32 #define OPENCV_FLANN_NNINDEX_H
joeverbout 0:ea44dc9ed014 33
joeverbout 0:ea44dc9ed014 34 #include "general.h"
joeverbout 0:ea44dc9ed014 35 #include "matrix.h"
joeverbout 0:ea44dc9ed014 36 #include "result_set.h"
joeverbout 0:ea44dc9ed014 37 #include "params.h"
joeverbout 0:ea44dc9ed014 38
joeverbout 0:ea44dc9ed014 39 namespace cvflann
joeverbout 0:ea44dc9ed014 40 {
joeverbout 0:ea44dc9ed014 41
joeverbout 0:ea44dc9ed014 42 /**
joeverbout 0:ea44dc9ed014 43 * Nearest-neighbour index base class
joeverbout 0:ea44dc9ed014 44 */
joeverbout 0:ea44dc9ed014 45 template <typename Distance>
joeverbout 0:ea44dc9ed014 46 class NNIndex
joeverbout 0:ea44dc9ed014 47 {
joeverbout 0:ea44dc9ed014 48 typedef typename Distance::ElementType ElementType;
joeverbout 0:ea44dc9ed014 49 typedef typename Distance::ResultType DistanceType;
joeverbout 0:ea44dc9ed014 50
joeverbout 0:ea44dc9ed014 51 public:
joeverbout 0:ea44dc9ed014 52
joeverbout 0:ea44dc9ed014 53 virtual ~NNIndex() {}
joeverbout 0:ea44dc9ed014 54
joeverbout 0:ea44dc9ed014 55 /**
joeverbout 0:ea44dc9ed014 56 * \brief Builds the index
joeverbout 0:ea44dc9ed014 57 */
joeverbout 0:ea44dc9ed014 58 virtual void buildIndex() = 0;
joeverbout 0:ea44dc9ed014 59
joeverbout 0:ea44dc9ed014 60 /**
joeverbout 0:ea44dc9ed014 61 * \brief Perform k-nearest neighbor search
joeverbout 0:ea44dc9ed014 62 * \param[in] queries The query points for which to find the nearest neighbors
joeverbout 0:ea44dc9ed014 63 * \param[out] indices The indices of the nearest neighbors found
joeverbout 0:ea44dc9ed014 64 * \param[out] dists Distances to the nearest neighbors found
joeverbout 0:ea44dc9ed014 65 * \param[in] knn Number of nearest neighbors to return
joeverbout 0:ea44dc9ed014 66 * \param[in] params Search parameters
joeverbout 0:ea44dc9ed014 67 */
joeverbout 0:ea44dc9ed014 68 virtual void knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, int knn, const SearchParams& params)
joeverbout 0:ea44dc9ed014 69 {
joeverbout 0:ea44dc9ed014 70 assert(queries.cols == veclen());
joeverbout 0:ea44dc9ed014 71 assert(indices.rows >= queries.rows);
joeverbout 0:ea44dc9ed014 72 assert(dists.rows >= queries.rows);
joeverbout 0:ea44dc9ed014 73 assert(int(indices.cols) >= knn);
joeverbout 0:ea44dc9ed014 74 assert(int(dists.cols) >= knn);
joeverbout 0:ea44dc9ed014 75
joeverbout 0:ea44dc9ed014 76 #if 0
joeverbout 0:ea44dc9ed014 77 KNNResultSet<DistanceType> resultSet(knn);
joeverbout 0:ea44dc9ed014 78 for (size_t i = 0; i < queries.rows; i++) {
joeverbout 0:ea44dc9ed014 79 resultSet.init(indices[i], dists[i]);
joeverbout 0:ea44dc9ed014 80 findNeighbors(resultSet, queries[i], params);
joeverbout 0:ea44dc9ed014 81 }
joeverbout 0:ea44dc9ed014 82 #else
joeverbout 0:ea44dc9ed014 83 KNNUniqueResultSet<DistanceType> resultSet(knn);
joeverbout 0:ea44dc9ed014 84 for (size_t i = 0; i < queries.rows; i++) {
joeverbout 0:ea44dc9ed014 85 resultSet.clear();
joeverbout 0:ea44dc9ed014 86 findNeighbors(resultSet, queries[i], params);
joeverbout 0:ea44dc9ed014 87 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices[i], dists[i], knn);
joeverbout 0:ea44dc9ed014 88 else resultSet.copy(indices[i], dists[i], knn);
joeverbout 0:ea44dc9ed014 89 }
joeverbout 0:ea44dc9ed014 90 #endif
joeverbout 0:ea44dc9ed014 91 }
joeverbout 0:ea44dc9ed014 92
joeverbout 0:ea44dc9ed014 93 /**
joeverbout 0:ea44dc9ed014 94 * \brief Perform radius search
joeverbout 0:ea44dc9ed014 95 * \param[in] query The query point
joeverbout 0:ea44dc9ed014 96 * \param[out] indices The indinces of the neighbors found within the given radius
joeverbout 0:ea44dc9ed014 97 * \param[out] dists The distances to the nearest neighbors found
joeverbout 0:ea44dc9ed014 98 * \param[in] radius The radius used for search
joeverbout 0:ea44dc9ed014 99 * \param[in] params Search parameters
joeverbout 0:ea44dc9ed014 100 * \returns Number of neighbors found
joeverbout 0:ea44dc9ed014 101 */
joeverbout 0:ea44dc9ed014 102 virtual int radiusSearch(const Matrix<ElementType>& query, Matrix<int>& indices, Matrix<DistanceType>& dists, float radius, const SearchParams& params)
joeverbout 0:ea44dc9ed014 103 {
joeverbout 0:ea44dc9ed014 104 if (query.rows != 1) {
joeverbout 0:ea44dc9ed014 105 fprintf(stderr, "I can only search one feature at a time for range search\n");
joeverbout 0:ea44dc9ed014 106 return -1;
joeverbout 0:ea44dc9ed014 107 }
joeverbout 0:ea44dc9ed014 108 assert(query.cols == veclen());
joeverbout 0:ea44dc9ed014 109 assert(indices.cols == dists.cols);
joeverbout 0:ea44dc9ed014 110
joeverbout 0:ea44dc9ed014 111 int n = 0;
joeverbout 0:ea44dc9ed014 112 int* indices_ptr = NULL;
joeverbout 0:ea44dc9ed014 113 DistanceType* dists_ptr = NULL;
joeverbout 0:ea44dc9ed014 114 if (indices.cols > 0) {
joeverbout 0:ea44dc9ed014 115 n = (int)indices.cols;
joeverbout 0:ea44dc9ed014 116 indices_ptr = indices[0];
joeverbout 0:ea44dc9ed014 117 dists_ptr = dists[0];
joeverbout 0:ea44dc9ed014 118 }
joeverbout 0:ea44dc9ed014 119
joeverbout 0:ea44dc9ed014 120 RadiusUniqueResultSet<DistanceType> resultSet((DistanceType)radius);
joeverbout 0:ea44dc9ed014 121 resultSet.clear();
joeverbout 0:ea44dc9ed014 122 findNeighbors(resultSet, query[0], params);
joeverbout 0:ea44dc9ed014 123 if (n>0) {
joeverbout 0:ea44dc9ed014 124 if (get_param(params,"sorted",true)) resultSet.sortAndCopy(indices_ptr, dists_ptr, n);
joeverbout 0:ea44dc9ed014 125 else resultSet.copy(indices_ptr, dists_ptr, n);
joeverbout 0:ea44dc9ed014 126 }
joeverbout 0:ea44dc9ed014 127
joeverbout 0:ea44dc9ed014 128 return (int)resultSet.size();
joeverbout 0:ea44dc9ed014 129 }
joeverbout 0:ea44dc9ed014 130
joeverbout 0:ea44dc9ed014 131 /**
joeverbout 0:ea44dc9ed014 132 * \brief Saves the index to a stream
joeverbout 0:ea44dc9ed014 133 * \param stream The stream to save the index to
joeverbout 0:ea44dc9ed014 134 */
joeverbout 0:ea44dc9ed014 135 virtual void saveIndex(FILE* stream) = 0;
joeverbout 0:ea44dc9ed014 136
joeverbout 0:ea44dc9ed014 137 /**
joeverbout 0:ea44dc9ed014 138 * \brief Loads the index from a stream
joeverbout 0:ea44dc9ed014 139 * \param stream The stream from which the index is loaded
joeverbout 0:ea44dc9ed014 140 */
joeverbout 0:ea44dc9ed014 141 virtual void loadIndex(FILE* stream) = 0;
joeverbout 0:ea44dc9ed014 142
joeverbout 0:ea44dc9ed014 143 /**
joeverbout 0:ea44dc9ed014 144 * \returns number of features in this index.
joeverbout 0:ea44dc9ed014 145 */
joeverbout 0:ea44dc9ed014 146 virtual size_t size() const = 0;
joeverbout 0:ea44dc9ed014 147
joeverbout 0:ea44dc9ed014 148 /**
joeverbout 0:ea44dc9ed014 149 * \returns The dimensionality of the features in this index.
joeverbout 0:ea44dc9ed014 150 */
joeverbout 0:ea44dc9ed014 151 virtual size_t veclen() const = 0;
joeverbout 0:ea44dc9ed014 152
joeverbout 0:ea44dc9ed014 153 /**
joeverbout 0:ea44dc9ed014 154 * \returns The amount of memory (in bytes) used by the index.
joeverbout 0:ea44dc9ed014 155 */
joeverbout 0:ea44dc9ed014 156 virtual int usedMemory() const = 0;
joeverbout 0:ea44dc9ed014 157
joeverbout 0:ea44dc9ed014 158 /**
joeverbout 0:ea44dc9ed014 159 * \returns The index type (kdtree, kmeans,...)
joeverbout 0:ea44dc9ed014 160 */
joeverbout 0:ea44dc9ed014 161 virtual flann_algorithm_t getType() const = 0;
joeverbout 0:ea44dc9ed014 162
joeverbout 0:ea44dc9ed014 163 /**
joeverbout 0:ea44dc9ed014 164 * \returns The index parameters
joeverbout 0:ea44dc9ed014 165 */
joeverbout 0:ea44dc9ed014 166 virtual IndexParams getParameters() const = 0;
joeverbout 0:ea44dc9ed014 167
joeverbout 0:ea44dc9ed014 168
joeverbout 0:ea44dc9ed014 169 /**
joeverbout 0:ea44dc9ed014 170 * \brief Method that searches for nearest-neighbours
joeverbout 0:ea44dc9ed014 171 */
joeverbout 0:ea44dc9ed014 172 virtual void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) = 0;
joeverbout 0:ea44dc9ed014 173 };
joeverbout 0:ea44dc9ed014 174
joeverbout 0:ea44dc9ed014 175 }
joeverbout 0:ea44dc9ed014 176
joeverbout 0:ea44dc9ed014 177 #endif //OPENCV_FLANN_NNINDEX_H
joeverbout 0:ea44dc9ed014 178