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 * 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 GOODS 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
RyoheiHagimoto 0:0e0631af0305 30 #ifndef OPENCV_FLANN_HDF5_H_
RyoheiHagimoto 0:0e0631af0305 31 #define OPENCV_FLANN_HDF5_H_
RyoheiHagimoto 0:0e0631af0305 32
RyoheiHagimoto 0:0e0631af0305 33 #include <hdf5.h>
RyoheiHagimoto 0:0e0631af0305 34
RyoheiHagimoto 0:0e0631af0305 35 #include "matrix.h"
RyoheiHagimoto 0:0e0631af0305 36
RyoheiHagimoto 0:0e0631af0305 37
RyoheiHagimoto 0:0e0631af0305 38 namespace cvflann
RyoheiHagimoto 0:0e0631af0305 39 {
RyoheiHagimoto 0:0e0631af0305 40
RyoheiHagimoto 0:0e0631af0305 41 namespace
RyoheiHagimoto 0:0e0631af0305 42 {
RyoheiHagimoto 0:0e0631af0305 43
RyoheiHagimoto 0:0e0631af0305 44 template<typename T>
RyoheiHagimoto 0:0e0631af0305 45 hid_t get_hdf5_type()
RyoheiHagimoto 0:0e0631af0305 46 {
RyoheiHagimoto 0:0e0631af0305 47 throw FLANNException("Unsupported type for IO operations");
RyoheiHagimoto 0:0e0631af0305 48 }
RyoheiHagimoto 0:0e0631af0305 49
RyoheiHagimoto 0:0e0631af0305 50 template<>
RyoheiHagimoto 0:0e0631af0305 51 hid_t get_hdf5_type<char>() { return H5T_NATIVE_CHAR; }
RyoheiHagimoto 0:0e0631af0305 52 template<>
RyoheiHagimoto 0:0e0631af0305 53 hid_t get_hdf5_type<unsigned char>() { return H5T_NATIVE_UCHAR; }
RyoheiHagimoto 0:0e0631af0305 54 template<>
RyoheiHagimoto 0:0e0631af0305 55 hid_t get_hdf5_type<short int>() { return H5T_NATIVE_SHORT; }
RyoheiHagimoto 0:0e0631af0305 56 template<>
RyoheiHagimoto 0:0e0631af0305 57 hid_t get_hdf5_type<unsigned short int>() { return H5T_NATIVE_USHORT; }
RyoheiHagimoto 0:0e0631af0305 58 template<>
RyoheiHagimoto 0:0e0631af0305 59 hid_t get_hdf5_type<int>() { return H5T_NATIVE_INT; }
RyoheiHagimoto 0:0e0631af0305 60 template<>
RyoheiHagimoto 0:0e0631af0305 61 hid_t get_hdf5_type<unsigned int>() { return H5T_NATIVE_UINT; }
RyoheiHagimoto 0:0e0631af0305 62 template<>
RyoheiHagimoto 0:0e0631af0305 63 hid_t get_hdf5_type<long>() { return H5T_NATIVE_LONG; }
RyoheiHagimoto 0:0e0631af0305 64 template<>
RyoheiHagimoto 0:0e0631af0305 65 hid_t get_hdf5_type<unsigned long>() { return H5T_NATIVE_ULONG; }
RyoheiHagimoto 0:0e0631af0305 66 template<>
RyoheiHagimoto 0:0e0631af0305 67 hid_t get_hdf5_type<float>() { return H5T_NATIVE_FLOAT; }
RyoheiHagimoto 0:0e0631af0305 68 template<>
RyoheiHagimoto 0:0e0631af0305 69 hid_t get_hdf5_type<double>() { return H5T_NATIVE_DOUBLE; }
RyoheiHagimoto 0:0e0631af0305 70 }
RyoheiHagimoto 0:0e0631af0305 71
RyoheiHagimoto 0:0e0631af0305 72
RyoheiHagimoto 0:0e0631af0305 73 #define CHECK_ERROR(x,y) if ((x)<0) throw FLANNException((y));
RyoheiHagimoto 0:0e0631af0305 74
RyoheiHagimoto 0:0e0631af0305 75 template<typename T>
RyoheiHagimoto 0:0e0631af0305 76 void save_to_file(const cvflann::Matrix<T>& dataset, const String& filename, const String& name)
RyoheiHagimoto 0:0e0631af0305 77 {
RyoheiHagimoto 0:0e0631af0305 78
RyoheiHagimoto 0:0e0631af0305 79 #if H5Eset_auto_vers == 2
RyoheiHagimoto 0:0e0631af0305 80 H5Eset_auto( H5E_DEFAULT, NULL, NULL );
RyoheiHagimoto 0:0e0631af0305 81 #else
RyoheiHagimoto 0:0e0631af0305 82 H5Eset_auto( NULL, NULL );
RyoheiHagimoto 0:0e0631af0305 83 #endif
RyoheiHagimoto 0:0e0631af0305 84
RyoheiHagimoto 0:0e0631af0305 85 herr_t status;
RyoheiHagimoto 0:0e0631af0305 86 hid_t file_id;
RyoheiHagimoto 0:0e0631af0305 87 file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 88 if (file_id < 0) {
RyoheiHagimoto 0:0e0631af0305 89 file_id = H5Fcreate(filename.c_str(), H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 90 }
RyoheiHagimoto 0:0e0631af0305 91 CHECK_ERROR(file_id,"Error creating hdf5 file.");
RyoheiHagimoto 0:0e0631af0305 92
RyoheiHagimoto 0:0e0631af0305 93 hsize_t dimsf[2]; // dataset dimensions
RyoheiHagimoto 0:0e0631af0305 94 dimsf[0] = dataset.rows;
RyoheiHagimoto 0:0e0631af0305 95 dimsf[1] = dataset.cols;
RyoheiHagimoto 0:0e0631af0305 96
RyoheiHagimoto 0:0e0631af0305 97 hid_t space_id = H5Screate_simple(2, dimsf, NULL);
RyoheiHagimoto 0:0e0631af0305 98 hid_t memspace_id = H5Screate_simple(2, dimsf, NULL);
RyoheiHagimoto 0:0e0631af0305 99
RyoheiHagimoto 0:0e0631af0305 100 hid_t dataset_id;
RyoheiHagimoto 0:0e0631af0305 101 #if H5Dcreate_vers == 2
RyoheiHagimoto 0:0e0631af0305 102 dataset_id = H5Dcreate2(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 103 #else
RyoheiHagimoto 0:0e0631af0305 104 dataset_id = H5Dcreate(file_id, name.c_str(), get_hdf5_type<T>(), space_id, H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 105 #endif
RyoheiHagimoto 0:0e0631af0305 106
RyoheiHagimoto 0:0e0631af0305 107 if (dataset_id<0) {
RyoheiHagimoto 0:0e0631af0305 108 #if H5Dopen_vers == 2
RyoheiHagimoto 0:0e0631af0305 109 dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 110 #else
RyoheiHagimoto 0:0e0631af0305 111 dataset_id = H5Dopen(file_id, name.c_str());
RyoheiHagimoto 0:0e0631af0305 112 #endif
RyoheiHagimoto 0:0e0631af0305 113 }
RyoheiHagimoto 0:0e0631af0305 114 CHECK_ERROR(dataset_id,"Error creating or opening dataset in file.");
RyoheiHagimoto 0:0e0631af0305 115
RyoheiHagimoto 0:0e0631af0305 116 status = H5Dwrite(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, H5P_DEFAULT, dataset.data );
RyoheiHagimoto 0:0e0631af0305 117 CHECK_ERROR(status, "Error writing to dataset");
RyoheiHagimoto 0:0e0631af0305 118
RyoheiHagimoto 0:0e0631af0305 119 H5Sclose(memspace_id);
RyoheiHagimoto 0:0e0631af0305 120 H5Sclose(space_id);
RyoheiHagimoto 0:0e0631af0305 121 H5Dclose(dataset_id);
RyoheiHagimoto 0:0e0631af0305 122 H5Fclose(file_id);
RyoheiHagimoto 0:0e0631af0305 123
RyoheiHagimoto 0:0e0631af0305 124 }
RyoheiHagimoto 0:0e0631af0305 125
RyoheiHagimoto 0:0e0631af0305 126
RyoheiHagimoto 0:0e0631af0305 127 template<typename T>
RyoheiHagimoto 0:0e0631af0305 128 void load_from_file(cvflann::Matrix<T>& dataset, const String& filename, const String& name)
RyoheiHagimoto 0:0e0631af0305 129 {
RyoheiHagimoto 0:0e0631af0305 130 herr_t status;
RyoheiHagimoto 0:0e0631af0305 131 hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 132 CHECK_ERROR(file_id,"Error opening hdf5 file.");
RyoheiHagimoto 0:0e0631af0305 133
RyoheiHagimoto 0:0e0631af0305 134 hid_t dataset_id;
RyoheiHagimoto 0:0e0631af0305 135 #if H5Dopen_vers == 2
RyoheiHagimoto 0:0e0631af0305 136 dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 137 #else
RyoheiHagimoto 0:0e0631af0305 138 dataset_id = H5Dopen(file_id, name.c_str());
RyoheiHagimoto 0:0e0631af0305 139 #endif
RyoheiHagimoto 0:0e0631af0305 140 CHECK_ERROR(dataset_id,"Error opening dataset in file.");
RyoheiHagimoto 0:0e0631af0305 141
RyoheiHagimoto 0:0e0631af0305 142 hid_t space_id = H5Dget_space(dataset_id);
RyoheiHagimoto 0:0e0631af0305 143
RyoheiHagimoto 0:0e0631af0305 144 hsize_t dims_out[2];
RyoheiHagimoto 0:0e0631af0305 145 H5Sget_simple_extent_dims(space_id, dims_out, NULL);
RyoheiHagimoto 0:0e0631af0305 146
RyoheiHagimoto 0:0e0631af0305 147 dataset = cvflann::Matrix<T>(new T[dims_out[0]*dims_out[1]], dims_out[0], dims_out[1]);
RyoheiHagimoto 0:0e0631af0305 148
RyoheiHagimoto 0:0e0631af0305 149 status = H5Dread(dataset_id, get_hdf5_type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, dataset[0]);
RyoheiHagimoto 0:0e0631af0305 150 CHECK_ERROR(status, "Error reading dataset");
RyoheiHagimoto 0:0e0631af0305 151
RyoheiHagimoto 0:0e0631af0305 152 H5Sclose(space_id);
RyoheiHagimoto 0:0e0631af0305 153 H5Dclose(dataset_id);
RyoheiHagimoto 0:0e0631af0305 154 H5Fclose(file_id);
RyoheiHagimoto 0:0e0631af0305 155 }
RyoheiHagimoto 0:0e0631af0305 156
RyoheiHagimoto 0:0e0631af0305 157
RyoheiHagimoto 0:0e0631af0305 158 #ifdef HAVE_MPI
RyoheiHagimoto 0:0e0631af0305 159
RyoheiHagimoto 0:0e0631af0305 160 namespace mpi
RyoheiHagimoto 0:0e0631af0305 161 {
RyoheiHagimoto 0:0e0631af0305 162 /**
RyoheiHagimoto 0:0e0631af0305 163 * Loads a the hyperslice corresponding to this processor from a hdf5 file.
RyoheiHagimoto 0:0e0631af0305 164 * @param flann_dataset Dataset where the data is loaded
RyoheiHagimoto 0:0e0631af0305 165 * @param filename HDF5 file name
RyoheiHagimoto 0:0e0631af0305 166 * @param name Name of dataset inside file
RyoheiHagimoto 0:0e0631af0305 167 */
RyoheiHagimoto 0:0e0631af0305 168 template<typename T>
RyoheiHagimoto 0:0e0631af0305 169 void load_from_file(cvflann::Matrix<T>& dataset, const String& filename, const String& name)
RyoheiHagimoto 0:0e0631af0305 170 {
RyoheiHagimoto 0:0e0631af0305 171 MPI_Comm comm = MPI_COMM_WORLD;
RyoheiHagimoto 0:0e0631af0305 172 MPI_Info info = MPI_INFO_NULL;
RyoheiHagimoto 0:0e0631af0305 173
RyoheiHagimoto 0:0e0631af0305 174 int mpi_size, mpi_rank;
RyoheiHagimoto 0:0e0631af0305 175 MPI_Comm_size(comm, &mpi_size);
RyoheiHagimoto 0:0e0631af0305 176 MPI_Comm_rank(comm, &mpi_rank);
RyoheiHagimoto 0:0e0631af0305 177
RyoheiHagimoto 0:0e0631af0305 178 herr_t status;
RyoheiHagimoto 0:0e0631af0305 179
RyoheiHagimoto 0:0e0631af0305 180 hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
RyoheiHagimoto 0:0e0631af0305 181 H5Pset_fapl_mpio(plist_id, comm, info);
RyoheiHagimoto 0:0e0631af0305 182 hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, plist_id);
RyoheiHagimoto 0:0e0631af0305 183 CHECK_ERROR(file_id,"Error opening hdf5 file.");
RyoheiHagimoto 0:0e0631af0305 184 H5Pclose(plist_id);
RyoheiHagimoto 0:0e0631af0305 185 hid_t dataset_id;
RyoheiHagimoto 0:0e0631af0305 186 #if H5Dopen_vers == 2
RyoheiHagimoto 0:0e0631af0305 187 dataset_id = H5Dopen2(file_id, name.c_str(), H5P_DEFAULT);
RyoheiHagimoto 0:0e0631af0305 188 #else
RyoheiHagimoto 0:0e0631af0305 189 dataset_id = H5Dopen(file_id, name.c_str());
RyoheiHagimoto 0:0e0631af0305 190 #endif
RyoheiHagimoto 0:0e0631af0305 191 CHECK_ERROR(dataset_id,"Error opening dataset in file.");
RyoheiHagimoto 0:0e0631af0305 192
RyoheiHagimoto 0:0e0631af0305 193 hid_t space_id = H5Dget_space(dataset_id);
RyoheiHagimoto 0:0e0631af0305 194 hsize_t dims[2];
RyoheiHagimoto 0:0e0631af0305 195 H5Sget_simple_extent_dims(space_id, dims, NULL);
RyoheiHagimoto 0:0e0631af0305 196
RyoheiHagimoto 0:0e0631af0305 197 hsize_t count[2];
RyoheiHagimoto 0:0e0631af0305 198 hsize_t offset[2];
RyoheiHagimoto 0:0e0631af0305 199
RyoheiHagimoto 0:0e0631af0305 200 hsize_t item_cnt = dims[0]/mpi_size+(dims[0]%mpi_size==0 ? 0 : 1);
RyoheiHagimoto 0:0e0631af0305 201 hsize_t cnt = (mpi_rank<mpi_size-1 ? item_cnt : dims[0]-item_cnt*(mpi_size-1));
RyoheiHagimoto 0:0e0631af0305 202
RyoheiHagimoto 0:0e0631af0305 203 count[0] = cnt;
RyoheiHagimoto 0:0e0631af0305 204 count[1] = dims[1];
RyoheiHagimoto 0:0e0631af0305 205 offset[0] = mpi_rank*item_cnt;
RyoheiHagimoto 0:0e0631af0305 206 offset[1] = 0;
RyoheiHagimoto 0:0e0631af0305 207
RyoheiHagimoto 0:0e0631af0305 208 hid_t memspace_id = H5Screate_simple(2,count,NULL);
RyoheiHagimoto 0:0e0631af0305 209
RyoheiHagimoto 0:0e0631af0305 210 H5Sselect_hyperslab(space_id, H5S_SELECT_SET, offset, NULL, count, NULL);
RyoheiHagimoto 0:0e0631af0305 211
RyoheiHagimoto 0:0e0631af0305 212 dataset.rows = count[0];
RyoheiHagimoto 0:0e0631af0305 213 dataset.cols = count[1];
RyoheiHagimoto 0:0e0631af0305 214 dataset.data = new T[dataset.rows*dataset.cols];
RyoheiHagimoto 0:0e0631af0305 215
RyoheiHagimoto 0:0e0631af0305 216 plist_id = H5Pcreate(H5P_DATASET_XFER);
RyoheiHagimoto 0:0e0631af0305 217 H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
RyoheiHagimoto 0:0e0631af0305 218 status = H5Dread(dataset_id, get_hdf5_type<T>(), memspace_id, space_id, plist_id, dataset.data);
RyoheiHagimoto 0:0e0631af0305 219 CHECK_ERROR(status, "Error reading dataset");
RyoheiHagimoto 0:0e0631af0305 220
RyoheiHagimoto 0:0e0631af0305 221 H5Pclose(plist_id);
RyoheiHagimoto 0:0e0631af0305 222 H5Sclose(space_id);
RyoheiHagimoto 0:0e0631af0305 223 H5Sclose(memspace_id);
RyoheiHagimoto 0:0e0631af0305 224 H5Dclose(dataset_id);
RyoheiHagimoto 0:0e0631af0305 225 H5Fclose(file_id);
RyoheiHagimoto 0:0e0631af0305 226 }
RyoheiHagimoto 0:0e0631af0305 227 }
RyoheiHagimoto 0:0e0631af0305 228 #endif // HAVE_MPI
RyoheiHagimoto 0:0e0631af0305 229 } // namespace cvflann::mpi
RyoheiHagimoto 0:0e0631af0305 230
RyoheiHagimoto 0:0e0631af0305 231 #endif /* OPENCV_FLANN_HDF5_H_ */