|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +/** |
| 6 | + * @file create_read_write.cpp |
| 7 | + * @author Fangjun Kuang <csukuangfj dot at gmail dot com> |
| 8 | + * @date December 2017 |
| 9 | + * |
| 10 | + * @brief It demonstrates how to create a dataset, how |
| 11 | + * to write a cv::Mat to the dataset and how to |
| 12 | + * read a cv::Mat from it. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#ifdef __GNUC__ |
| 17 | +# pragma GCC diagnostic ignored "-Wmissing-declarations" |
| 18 | +# if defined __clang__ || defined __APPLE__ |
| 19 | +# pragma GCC diagnostic ignored "-Wmissing-prototypes" |
| 20 | +# pragma GCC diagnostic ignored "-Wextra" |
| 21 | +# endif |
| 22 | +#endif |
| 23 | + |
| 24 | +//! [tutorial] |
| 25 | +#include <iostream> |
| 26 | + |
| 27 | +#include <opencv2/core.hpp> |
| 28 | +#include <opencv2/hdf.hpp> |
| 29 | + |
| 30 | +using namespace cv; |
| 31 | + |
| 32 | +void write_root_group_single_channel() |
| 33 | +{ |
| 34 | + String filename = "root_group_single_channel.h5"; |
| 35 | + String dataset_name = "/single"; // Note that it is a child of the root group / |
| 36 | + |
| 37 | + // prepare data |
| 38 | + Mat data; |
| 39 | + data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5, 6); |
| 40 | + |
| 41 | + //! [tutorial_open_file] |
| 42 | + Ptr<hdf::HDF5> h5io = hdf::open(filename); |
| 43 | + //! [tutorial_open_file] |
| 44 | + |
| 45 | + //! [tutorial_write_root_single_channel] |
| 46 | + // write data to the given dataset |
| 47 | + // the dataset "/single" is created automatically, since it is a child of the root |
| 48 | + h5io->dswrite(data, dataset_name); |
| 49 | + //! [tutorial_write_root_single_channel] |
| 50 | + |
| 51 | + //! [tutorial_read_dataset] |
| 52 | + Mat expected; |
| 53 | + h5io->dsread(expected, dataset_name); |
| 54 | + //! [tutorial_read_dataset] |
| 55 | + |
| 56 | + //! [tutorial_check_result] |
| 57 | + double diff = norm(data - expected); |
| 58 | + CV_Assert(abs(diff) < 1e-10); |
| 59 | + //! [tutorial_check_result] |
| 60 | + |
| 61 | + h5io->close(); |
| 62 | +} |
| 63 | + |
| 64 | +void write_single_channel() |
| 65 | +{ |
| 66 | + String filename = "single_channel.h5"; |
| 67 | + String parent_name = "/data"; |
| 68 | + String dataset_name = parent_name + "/single"; |
| 69 | + |
| 70 | + // prepare data |
| 71 | + Mat data; |
| 72 | + data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5); |
| 73 | + |
| 74 | + Ptr<hdf::HDF5> h5io = hdf::open(filename); |
| 75 | + |
| 76 | + //! [tutorial_create_dataset] |
| 77 | + // first we need to create the parent group |
| 78 | + if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name); |
| 79 | + |
| 80 | + // create the dataset if it not exists |
| 81 | + if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name); |
| 82 | + //! [tutorial_create_dataset] |
| 83 | + |
| 84 | + // the following is the same with the above function write_root_group_single_channel() |
| 85 | + |
| 86 | + h5io->dswrite(data, dataset_name); |
| 87 | + |
| 88 | + Mat expected; |
| 89 | + h5io->dsread(expected, dataset_name); |
| 90 | + |
| 91 | + double diff = norm(data - expected); |
| 92 | + CV_Assert(abs(diff) < 1e-10); |
| 93 | + |
| 94 | + h5io->close(); |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * creating, reading and writing multiple-channel matrices |
| 99 | + * are the same with single channel matrices |
| 100 | + */ |
| 101 | +void write_multiple_channels() |
| 102 | +{ |
| 103 | + String filename = "two_channels.h5"; |
| 104 | + String parent_name = "/data"; |
| 105 | + String dataset_name = parent_name + "/two_channels"; |
| 106 | + |
| 107 | + // prepare data |
| 108 | + Mat data(2, 3, CV_32SC2); |
| 109 | + for (size_t i = 0; i < data.total()*data.channels(); i++) |
| 110 | + ((int*) data.data)[i] = (int)i; |
| 111 | + |
| 112 | + Ptr<hdf::HDF5> h5io = hdf::open(filename); |
| 113 | + |
| 114 | + // first we need to create the parent group |
| 115 | + if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name); |
| 116 | + |
| 117 | + // create the dataset if it not exists |
| 118 | + if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name); |
| 119 | + |
| 120 | + // the following is the same with the above function write_root_group_single_channel() |
| 121 | + |
| 122 | + h5io->dswrite(data, dataset_name); |
| 123 | + |
| 124 | + Mat expected; |
| 125 | + h5io->dsread(expected, dataset_name); |
| 126 | + |
| 127 | + double diff = norm(data - expected); |
| 128 | + CV_Assert(abs(diff) < 1e-10); |
| 129 | + |
| 130 | + h5io->close(); |
| 131 | +} |
| 132 | + |
| 133 | +int main() |
| 134 | +{ |
| 135 | + write_root_group_single_channel(); |
| 136 | + |
| 137 | + write_single_channel(); |
| 138 | + |
| 139 | + write_multiple_channels(); |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | +//! [tutorial] |
0 commit comments