Goal
This tutorial shows you:
- How to create a dataset?
- How to write a
cv::Mat
to a dataset?
- How to read a
cv::Mat
from a dataset?
- Note
- Currently, it supports only reading and writing cv::Mat and the matrix should be continuous in memory. Supports for other data types have not been implemented yet.
Source Code
The following code demonstrates writing a single channel matrix and a two-channel matrix to datasets and then reading them back.
You can download the code from here or find it in the file modules/hdf/samples/create_read_write_datasets.cpp
of the opencv_contrib source code library.
#include <iostream>
static void write_root_group_single_channel()
{
String filename =
"root_group_single_channel.h5";
String dataset_name =
"/single";
h5io->dswrite(data, dataset_name);
h5io->dsread(expected, dataset_name);
double diff = norm(data - expected);
h5io->close();
}
static void write_single_channel()
{
String filename =
"single_channel.h5";
String dataset_name = parent_name +
"/single";
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.
rows, data.
cols, data.
type(), dataset_name);
h5io->dswrite(data, dataset_name);
h5io->dsread(expected, dataset_name);
double diff = norm(data - expected);
h5io->close();
}
static void write_multiple_channels()
{
String filename =
"two_channels.h5";
String dataset_name = parent_name +
"/two_channels";
((
int*) data.
data)[i] = (
int)i;
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.
rows, data.
cols, data.
type(), dataset_name);
h5io->dswrite(data, dataset_name);
h5io->dsread(expected, dataset_name);
double diff = norm(data - expected);
h5io->close();
}
{
write_root_group_single_channel();
write_single_channel();
write_multiple_channels();
return 0;
}
Template matrix class derived from Mat.
Definition mat.hpp:2517
n-dimensional dense array class
Definition mat.hpp:950
uchar * data
pointer to the data
Definition mat.hpp:2427
int channels() const
Returns the number of matrix channels.
int cols
Definition mat.hpp:2424
size_t total() const
Returns the total number of array elements.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2424
int type() const
Returns the type of a matrix element.
std::string String
Definition cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
#define CV_32SC2
Definition interface.h:124
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition exception.hpp:198
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Explanation
The first step for creating a dataset is to open the file
For the function write_root_group_single_channel()
, since the dataset name is /single
, which is inside the root group, we can use
h5io->dswrite(data, dataset_name);
to write the data directly to the dataset without the need of creating it beforehand. Because it is created inside cv::hdf::HDF5::dswrite() automatically.
- Warning
- This applies only to datasets that reside inside the root group.
Of course, we can create the dataset by ourselves:
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.
rows, data.
cols, data.
type(), dataset_name);
To read data from a dataset, we use
h5io->dsread(expected, dataset_name);
by specifying the name of the dataset.
We can check that the data read out is exactly the data written before by using
double diff = norm(data - expected);
Results
Figure 1 shows the result visualized using the tool HDFView for the file root_group_single_channel
. The results of matrices for datasets that are not the direct children of the root group are given in Figure 2 and Figure 3, respectively.
Figure 1: Result for writing a single channel matrix to a dataset inside the root group
Figure 2: Result for writing a single channel matrix to a dataset not in the root group
Figure 3: Result for writing a two-channel matrix to a dataset not in the root group