h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5Easy_opencv.hpp
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#ifndef H5EASY_BITS_OPENCV_HPP
10#define H5EASY_BITS_OPENCV_HPP
11
12#include "../H5Easy.hpp"
13#include "H5Easy_misc.hpp"
14#include "H5Easy_scalar.hpp"
15
16#ifdef H5GT_USE_OPENCV
17
18namespace H5Easy {
19
20namespace detail {
21
22template <class T>
23struct is_opencv : std::false_type {};
24template <class T>
25struct is_opencv<cv::Mat_<T>> : std::true_type {};
26
27template <typename T>
28struct io_impl<T, typename std::enable_if<is_opencv<T>::value>::type> {
29
30 inline static std::vector<size_t> shape(const T& data)
31 {
32 return std::vector<size_t>{static_cast<size_t>(data.rows),
33 static_cast<size_t>(data.cols)};
34 }
35
36 inline static std::vector<int> shape(const File& file,
37 const std::string& path,
38 std::vector<size_t> dims)
39 {
40 if (dims.size() == 1) {
41 return std::vector<int>{static_cast<int>(dims[0]), 1ul};
42 }
43 if (dims.size() == 2) {
44 return std::vector<int>{static_cast<int>(dims[0]),
45 static_cast<int>(dims[1])};
46 }
47
48 throw detail::error(file, path, "H5Easy::load: Inconsistent rank");
49 }
50
51 inline static DataSet dump(File& file,
52 const std::string& path,
53 const T& data,
54 const DumpOptions& options) {
55 using value_type = typename T::value_type;
56 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
57 std::vector<value_type> v(data.begin(), data.end());
58 dataset.write_raw(v.data());
59 if (options.flush()) {
60 file.flush();
61 }
62 return dataset;
63 }
64
65 inline static T load(const File& file, const std::string& path) {
66 using value_type = typename T::value_type;
67 DataSet dataset = file.getDataSet(path);
68 std::vector<int> dims = shape(file, path, dataset.getDimensions());
69 T data(dims[0], dims[1]);
70 dataset.read(reinterpret_cast<value_type*>(data.data));
71 return data;
72 }
73
74 inline static Attribute dumpAttribute(File& file,
75 const std::string& path,
76 const std::string& key,
77 const T& data,
78 const DumpOptions& options) {
79 using value_type = typename T::value_type;
80 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
81 std::vector<value_type> v(data.begin(), data.end());
82 attribute.write_raw(v.data());
83 if (options.flush()) {
84 file.flush();
85 }
86 return attribute;
87 }
88
89 inline static T loadAttribute(const File& file,
90 const std::string& path,
91 const std::string& key) {
92 using value_type = typename T::value_type;
93 DataSet dataset = file.getDataSet(path);
94 Attribute attribute = dataset.getAttribute(key);
95 DataSpace dataspace = attribute.getSpace();
96 std::vector<int> dims = shape(file, path, dataspace.getDimensions());
97 T data(dims[0], dims[1]);
98 attribute.read(reinterpret_cast<value_type*>(data.data));
99 return data;
100 }
101};
102
103} // namespace detail
104} // namespace H5Easy
105
106#endif // H5GT_USE_OPENCV
107#endif // H5EASY_BITS_OPENCV_HPP