HighFive 2.10.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Easy_opencv.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#pragma once
10
11#include "../H5Easy.hpp"
12#include "H5Easy_misc.hpp"
13#include "H5Easy_scalar.hpp"
14
15#ifdef H5_USE_OPENCV
16
17namespace H5Easy {
18
19namespace detail {
20
21template <class T>
22struct is_opencv: std::false_type {};
23template <class T>
24struct is_opencv<cv::Mat_<T>>: std::true_type {};
25
26template <typename T>
27struct io_impl<T, typename std::enable_if<is_opencv<T>::value>::type> {
28 inline static std::vector<size_t> shape(const T& data) {
29 return std::vector<size_t>{static_cast<size_t>(data.rows), static_cast<size_t>(data.cols)};
30 }
31
32 inline static std::vector<int> shape(const File& file,
33 const std::string& path,
34 std::vector<size_t> dims) {
35 if (dims.size() == 1) {
36 return std::vector<int>{static_cast<int>(dims[0]), 1ul};
37 }
38 if (dims.size() == 2) {
39 return std::vector<int>{static_cast<int>(dims[0]), static_cast<int>(dims[1])};
40 }
41
42 throw detail::error(file, path, "H5Easy::load: Inconsistent rank");
43 }
44
45 inline static DataSet dump(File& file,
46 const std::string& path,
47 const T& data,
48 const DumpOptions& options) {
49 using value_type = typename T::value_type;
50 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
51 std::vector<value_type> v(data.begin(), data.end());
52 dataset.write_raw(v.data());
53 if (options.flush()) {
54 file.flush();
55 }
56 return dataset;
57 }
58
59 inline static T load(const File& file, const std::string& path) {
60 using value_type = typename T::value_type;
61 DataSet dataset = file.getDataSet(path);
62 std::vector<int> dims = shape(file, path, dataset.getDimensions());
63 T data(dims[0], dims[1]);
64 dataset.read_raw(reinterpret_cast<value_type*>(data.data));
65 return data;
66 }
67
68 inline static Attribute dumpAttribute(File& file,
69 const std::string& path,
70 const std::string& key,
71 const T& data,
72 const DumpOptions& options) {
73 using value_type = typename T::value_type;
74 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
75 std::vector<value_type> v(data.begin(), data.end());
76 attribute.write_raw(v.data());
77 if (options.flush()) {
78 file.flush();
79 }
80 return attribute;
81 }
82
83 inline static T loadAttribute(const File& file,
84 const std::string& path,
85 const std::string& key) {
86 using value_type = typename T::value_type;
87 DataSet dataset = file.getDataSet(path);
88 Attribute attribute = dataset.getAttribute(key);
89 DataSpace dataspace = attribute.getSpace();
90 std::vector<int> dims = shape(file, path, dataspace.getDimensions());
91 T data(dims[0], dims[1]);
92 attribute.read_raw(reinterpret_cast<value_type*>(data.data));
93 return data;
94 }
95};
96
97} // namespace detail
98} // namespace H5Easy
99
100#endif // H5_USE_OPENCV
Read/dump DataSets or Attribute using a minimalistic syntax. To this end, the functions are templated...
Definition H5Easy.hpp:59
DataSet dump(File &file, const std::string &path, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) DataSet in an open HDF5 file.
Definition H5Easy_public.hpp:99
T loadAttribute(const File &file, const std::string &path, const std::string &key)
Load a Attribute in an open HDF5 file to an object (templated).
Definition H5Easy_public.hpp:166
Attribute dumpAttribute(File &file, const std::string &path, const std::string &key, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) Attribute in an open HDF5 file.
Definition H5Easy_public.hpp:148
T load(const File &file, const std::string &path, const std::vector< size_t > &idx)
Load entry {i, j, ...} from a DataSet in an open HDF5 file to a scalar.
Definition H5Easy_public.hpp:138