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 * Redistribution and use in source and binary forms, with or without
joeverbout 0:ea44dc9ed014 8 * modification, are permitted provided that the following conditions
joeverbout 0:ea44dc9ed014 9 * are met:
joeverbout 0:ea44dc9ed014 10 *
joeverbout 0:ea44dc9ed014 11 * 1. Redistributions of source code must retain the above copyright
joeverbout 0:ea44dc9ed014 12 * notice, this list of conditions and the following disclaimer.
joeverbout 0:ea44dc9ed014 13 * 2. Redistributions in binary form must reproduce the above copyright
joeverbout 0:ea44dc9ed014 14 * notice, this list of conditions and the following disclaimer in the
joeverbout 0:ea44dc9ed014 15 * documentation and/or other materials provided with the distribution.
joeverbout 0:ea44dc9ed014 16 *
joeverbout 0:ea44dc9ed014 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
joeverbout 0:ea44dc9ed014 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
joeverbout 0:ea44dc9ed014 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
joeverbout 0:ea44dc9ed014 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
joeverbout 0:ea44dc9ed014 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
joeverbout 0:ea44dc9ed014 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE NNIndexGOODS OR SERVICES; LOSS OF USE,
joeverbout 0:ea44dc9ed014 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
joeverbout 0:ea44dc9ed014 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
joeverbout 0:ea44dc9ed014 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
joeverbout 0:ea44dc9ed014 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
joeverbout 0:ea44dc9ed014 27 *************************************************************************/
joeverbout 0:ea44dc9ed014 28
joeverbout 0:ea44dc9ed014 29 #ifndef OPENCV_FLANN_SAVING_H_
joeverbout 0:ea44dc9ed014 30 #define OPENCV_FLANN_SAVING_H_
joeverbout 0:ea44dc9ed014 31
joeverbout 0:ea44dc9ed014 32 #include <cstring>
joeverbout 0:ea44dc9ed014 33 #include <vector>
joeverbout 0:ea44dc9ed014 34
joeverbout 0:ea44dc9ed014 35 #include "general.h"
joeverbout 0:ea44dc9ed014 36 #include "nn_index.h"
joeverbout 0:ea44dc9ed014 37
joeverbout 0:ea44dc9ed014 38 #ifdef FLANN_SIGNATURE_
joeverbout 0:ea44dc9ed014 39 #undef FLANN_SIGNATURE_
joeverbout 0:ea44dc9ed014 40 #endif
joeverbout 0:ea44dc9ed014 41 #define FLANN_SIGNATURE_ "FLANN_INDEX"
joeverbout 0:ea44dc9ed014 42
joeverbout 0:ea44dc9ed014 43 namespace cvflann
joeverbout 0:ea44dc9ed014 44 {
joeverbout 0:ea44dc9ed014 45
joeverbout 0:ea44dc9ed014 46 template <typename T>
joeverbout 0:ea44dc9ed014 47 struct Datatype {};
joeverbout 0:ea44dc9ed014 48 template<>
joeverbout 0:ea44dc9ed014 49 struct Datatype<char> { static flann_datatype_t type() { return FLANN_INT8; } };
joeverbout 0:ea44dc9ed014 50 template<>
joeverbout 0:ea44dc9ed014 51 struct Datatype<short> { static flann_datatype_t type() { return FLANN_INT16; } };
joeverbout 0:ea44dc9ed014 52 template<>
joeverbout 0:ea44dc9ed014 53 struct Datatype<int> { static flann_datatype_t type() { return FLANN_INT32; } };
joeverbout 0:ea44dc9ed014 54 template<>
joeverbout 0:ea44dc9ed014 55 struct Datatype<unsigned char> { static flann_datatype_t type() { return FLANN_UINT8; } };
joeverbout 0:ea44dc9ed014 56 template<>
joeverbout 0:ea44dc9ed014 57 struct Datatype<unsigned short> { static flann_datatype_t type() { return FLANN_UINT16; } };
joeverbout 0:ea44dc9ed014 58 template<>
joeverbout 0:ea44dc9ed014 59 struct Datatype<unsigned int> { static flann_datatype_t type() { return FLANN_UINT32; } };
joeverbout 0:ea44dc9ed014 60 template<>
joeverbout 0:ea44dc9ed014 61 struct Datatype<float> { static flann_datatype_t type() { return FLANN_FLOAT32; } };
joeverbout 0:ea44dc9ed014 62 template<>
joeverbout 0:ea44dc9ed014 63 struct Datatype<double> { static flann_datatype_t type() { return FLANN_FLOAT64; } };
joeverbout 0:ea44dc9ed014 64
joeverbout 0:ea44dc9ed014 65
joeverbout 0:ea44dc9ed014 66 /**
joeverbout 0:ea44dc9ed014 67 * Structure representing the index header.
joeverbout 0:ea44dc9ed014 68 */
joeverbout 0:ea44dc9ed014 69 struct IndexHeader
joeverbout 0:ea44dc9ed014 70 {
joeverbout 0:ea44dc9ed014 71 char signature[16];
joeverbout 0:ea44dc9ed014 72 char version[16];
joeverbout 0:ea44dc9ed014 73 flann_datatype_t data_type;
joeverbout 0:ea44dc9ed014 74 flann_algorithm_t index_type;
joeverbout 0:ea44dc9ed014 75 size_t rows;
joeverbout 0:ea44dc9ed014 76 size_t cols;
joeverbout 0:ea44dc9ed014 77 };
joeverbout 0:ea44dc9ed014 78
joeverbout 0:ea44dc9ed014 79 /**
joeverbout 0:ea44dc9ed014 80 * Saves index header to stream
joeverbout 0:ea44dc9ed014 81 *
joeverbout 0:ea44dc9ed014 82 * @param stream - Stream to save to
joeverbout 0:ea44dc9ed014 83 * @param index - The index to save
joeverbout 0:ea44dc9ed014 84 */
joeverbout 0:ea44dc9ed014 85 template<typename Distance>
joeverbout 0:ea44dc9ed014 86 void save_header(FILE* stream, const NNIndex<Distance>& index)
joeverbout 0:ea44dc9ed014 87 {
joeverbout 0:ea44dc9ed014 88 IndexHeader header;
joeverbout 0:ea44dc9ed014 89 memset(header.signature, 0, sizeof(header.signature));
joeverbout 0:ea44dc9ed014 90 strcpy(header.signature, FLANN_SIGNATURE_);
joeverbout 0:ea44dc9ed014 91 memset(header.version, 0, sizeof(header.version));
joeverbout 0:ea44dc9ed014 92 strcpy(header.version, FLANN_VERSION_);
joeverbout 0:ea44dc9ed014 93 header.data_type = Datatype<typename Distance::ElementType>::type();
joeverbout 0:ea44dc9ed014 94 header.index_type = index.getType();
joeverbout 0:ea44dc9ed014 95 header.rows = index.size();
joeverbout 0:ea44dc9ed014 96 header.cols = index.veclen();
joeverbout 0:ea44dc9ed014 97
joeverbout 0:ea44dc9ed014 98 std::fwrite(&header, sizeof(header),1,stream);
joeverbout 0:ea44dc9ed014 99 }
joeverbout 0:ea44dc9ed014 100
joeverbout 0:ea44dc9ed014 101
joeverbout 0:ea44dc9ed014 102 /**
joeverbout 0:ea44dc9ed014 103 *
joeverbout 0:ea44dc9ed014 104 * @param stream - Stream to load from
joeverbout 0:ea44dc9ed014 105 * @return Index header
joeverbout 0:ea44dc9ed014 106 */
joeverbout 0:ea44dc9ed014 107 inline IndexHeader load_header(FILE* stream)
joeverbout 0:ea44dc9ed014 108 {
joeverbout 0:ea44dc9ed014 109 IndexHeader header;
joeverbout 0:ea44dc9ed014 110 size_t read_size = fread(&header,sizeof(header),1,stream);
joeverbout 0:ea44dc9ed014 111
joeverbout 0:ea44dc9ed014 112 if (read_size!=(size_t)1) {
joeverbout 0:ea44dc9ed014 113 throw FLANNException("Invalid index file, cannot read");
joeverbout 0:ea44dc9ed014 114 }
joeverbout 0:ea44dc9ed014 115
joeverbout 0:ea44dc9ed014 116 if (strcmp(header.signature,FLANN_SIGNATURE_)!=0) {
joeverbout 0:ea44dc9ed014 117 throw FLANNException("Invalid index file, wrong signature");
joeverbout 0:ea44dc9ed014 118 }
joeverbout 0:ea44dc9ed014 119
joeverbout 0:ea44dc9ed014 120 return header;
joeverbout 0:ea44dc9ed014 121
joeverbout 0:ea44dc9ed014 122 }
joeverbout 0:ea44dc9ed014 123
joeverbout 0:ea44dc9ed014 124
joeverbout 0:ea44dc9ed014 125 template<typename T>
joeverbout 0:ea44dc9ed014 126 void save_value(FILE* stream, const T& value, size_t count = 1)
joeverbout 0:ea44dc9ed014 127 {
joeverbout 0:ea44dc9ed014 128 fwrite(&value, sizeof(value),count, stream);
joeverbout 0:ea44dc9ed014 129 }
joeverbout 0:ea44dc9ed014 130
joeverbout 0:ea44dc9ed014 131 template<typename T>
joeverbout 0:ea44dc9ed014 132 void save_value(FILE* stream, const cvflann::Matrix<T>& value)
joeverbout 0:ea44dc9ed014 133 {
joeverbout 0:ea44dc9ed014 134 fwrite(&value, sizeof(value),1, stream);
joeverbout 0:ea44dc9ed014 135 fwrite(value.data, sizeof(T),value.rows*value.cols, stream);
joeverbout 0:ea44dc9ed014 136 }
joeverbout 0:ea44dc9ed014 137
joeverbout 0:ea44dc9ed014 138 template<typename T>
joeverbout 0:ea44dc9ed014 139 void save_value(FILE* stream, const std::vector<T>& value)
joeverbout 0:ea44dc9ed014 140 {
joeverbout 0:ea44dc9ed014 141 size_t size = value.size();
joeverbout 0:ea44dc9ed014 142 fwrite(&size, sizeof(size_t), 1, stream);
joeverbout 0:ea44dc9ed014 143 fwrite(&value[0], sizeof(T), size, stream);
joeverbout 0:ea44dc9ed014 144 }
joeverbout 0:ea44dc9ed014 145
joeverbout 0:ea44dc9ed014 146 template<typename T>
joeverbout 0:ea44dc9ed014 147 void load_value(FILE* stream, T& value, size_t count = 1)
joeverbout 0:ea44dc9ed014 148 {
joeverbout 0:ea44dc9ed014 149 size_t read_cnt = fread(&value, sizeof(value), count, stream);
joeverbout 0:ea44dc9ed014 150 if (read_cnt != count) {
joeverbout 0:ea44dc9ed014 151 throw FLANNException("Cannot read from file");
joeverbout 0:ea44dc9ed014 152 }
joeverbout 0:ea44dc9ed014 153 }
joeverbout 0:ea44dc9ed014 154
joeverbout 0:ea44dc9ed014 155 template<typename T>
joeverbout 0:ea44dc9ed014 156 void load_value(FILE* stream, cvflann::Matrix<T>& value)
joeverbout 0:ea44dc9ed014 157 {
joeverbout 0:ea44dc9ed014 158 size_t read_cnt = fread(&value, sizeof(value), 1, stream);
joeverbout 0:ea44dc9ed014 159 if (read_cnt != 1) {
joeverbout 0:ea44dc9ed014 160 throw FLANNException("Cannot read from file");
joeverbout 0:ea44dc9ed014 161 }
joeverbout 0:ea44dc9ed014 162 value.data = new T[value.rows*value.cols];
joeverbout 0:ea44dc9ed014 163 read_cnt = fread(value.data, sizeof(T), value.rows*value.cols, stream);
joeverbout 0:ea44dc9ed014 164 if (read_cnt != (size_t)(value.rows*value.cols)) {
joeverbout 0:ea44dc9ed014 165 throw FLANNException("Cannot read from file");
joeverbout 0:ea44dc9ed014 166 }
joeverbout 0:ea44dc9ed014 167 }
joeverbout 0:ea44dc9ed014 168
joeverbout 0:ea44dc9ed014 169
joeverbout 0:ea44dc9ed014 170 template<typename T>
joeverbout 0:ea44dc9ed014 171 void load_value(FILE* stream, std::vector<T>& value)
joeverbout 0:ea44dc9ed014 172 {
joeverbout 0:ea44dc9ed014 173 size_t size;
joeverbout 0:ea44dc9ed014 174 size_t read_cnt = fread(&size, sizeof(size_t), 1, stream);
joeverbout 0:ea44dc9ed014 175 if (read_cnt!=1) {
joeverbout 0:ea44dc9ed014 176 throw FLANNException("Cannot read from file");
joeverbout 0:ea44dc9ed014 177 }
joeverbout 0:ea44dc9ed014 178 value.resize(size);
joeverbout 0:ea44dc9ed014 179 read_cnt = fread(&value[0], sizeof(T), size, stream);
joeverbout 0:ea44dc9ed014 180 if (read_cnt != size) {
joeverbout 0:ea44dc9ed014 181 throw FLANNException("Cannot read from file");
joeverbout 0:ea44dc9ed014 182 }
joeverbout 0:ea44dc9ed014 183 }
joeverbout 0:ea44dc9ed014 184
joeverbout 0:ea44dc9ed014 185 }
joeverbout 0:ea44dc9ed014 186
joeverbout 0:ea44dc9ed014 187 #endif /* OPENCV_FLANN_SAVING_H_ */
joeverbout 0:ea44dc9ed014 188