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