Skip to content

Commit 32aad03

Browse files
committed
Improve the HDF module.
- Some test cases are added - Two tutorials are written to show how to use it.
1 parent 53986aa commit 32aad03

File tree

16 files changed

+801
-174
lines changed

16 files changed

+801
-174
lines changed

modules/fuzzy/samples/fuzzy_filtering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* using "kernel2" with radius 100.
1212
*
1313
* Both kernels are created from linear function, using
14-
* linear interpolation (parametr ft:LINEAR).
14+
* linear interpolation (parameter ft:LINEAR).
1515
*/
1616

1717
#include "opencv2/core.hpp"
@@ -26,7 +26,7 @@ int main(void)
2626
// Input image
2727
Mat I = imread("input.png");
2828

29-
// Kernel cretion
29+
// Kernel creation
3030
Mat kernel1, kernel2;
3131

3232
ft::createKernel(ft::LINEAR, 3, kernel1, 3);
19 KB
Loading
27.9 KB
Loading
28 KB
Loading

modules/hdf/doc/pics/two_channels.png

30.4 KB
Loading

modules/hdf/include/opencv2/hdf.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ This module provides storage routines for Hierarchical Data Format objects.
4747
Hierarchical Data Format version 5
4848
--------------------------------------------------------
4949
50+
In order to use it, the hdf5 library has to be installed, which
51+
means cmake should find it using `find_package(HDF5)` .
52+
5053
5154
@}
5255
*/

modules/hdf/include/opencv2/hdf/hdf5.hpp

Lines changed: 110 additions & 105 deletions
Large diffs are not rendered by default.

modules/hdf/samples/create_groups.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_groups.cpp
7+
* @author Fangjun Kuang <csukuangfj dot at gmail dot com>
8+
* @date December 2017
9+
*
10+
* @brief It demonstrates how to create HDF5 groups and subgroups.
11+
*
12+
* Basic steps:
13+
* 1. Use hdf::open to create a HDF5 file
14+
* 2. Use HDF5::hlexists to check if a group exists or not
15+
* 3. Use HDF5::grcreate to create a group by specifying its name
16+
* 4. Use hdf::close to close a HDF5 file after modifying it
17+
*
18+
*/
19+
20+
//! [tutorial]
21+
#include <iostream>
22+
23+
#include <opencv2/core.hpp>
24+
#include <opencv2/hdf.hpp>
25+
26+
using namespace cv;
27+
28+
int main()
29+
{
30+
//! [create_group]
31+
32+
//! [tutorial_create_file]
33+
Ptr<hdf::HDF5> h5io = hdf::open("mytest.h5");
34+
//! [tutorial_create_file]
35+
36+
//! [tutorial_create_group]
37+
// "/" means the root group, which is always present
38+
if (!h5io->hlexists("/Group1"))
39+
h5io->grcreate("/Group1");
40+
else
41+
std::cout << "/Group1 has already been created, skip it.\n";
42+
//! [tutorial_create_group]
43+
44+
//! [tutorial_create_subgroup]
45+
// Note that Group1 has been created above, otherwise exception will occur
46+
if (!h5io->hlexists("/Group1/SubGroup1"))
47+
h5io->grcreate("/Group1/SubGroup1");
48+
else
49+
std::cout << "/Group1/SubGroup1 has already been created, skip it.\n";
50+
//! [tutorial_create_subgroup]
51+
52+
//! [tutorial_close_file]
53+
h5io->close();
54+
//! [tutorial_close_file]
55+
56+
//! [create_group]
57+
58+
return 0;
59+
}
60+
//! [tutorial]
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
//! [tutorial]
17+
#include <iostream>
18+
19+
#include <opencv2/core.hpp>
20+
#include <opencv2/hdf.hpp>
21+
22+
using namespace cv;
23+
24+
void write_root_group_single_channel()
25+
{
26+
String filename = "root_group_single_channel.h5";
27+
String dataset_name = "/single"; // Note that it is a child of the root group /
28+
29+
// prepare data
30+
Mat data;
31+
data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5, 6);
32+
33+
//! [tutorial_open_file]
34+
Ptr<hdf::HDF5> h5io = hdf::open(filename);
35+
//! [tutorial_open_file]
36+
37+
//! [tutorial_write_root_single_channel]
38+
// write data to the given dataset
39+
// the dataset "/single" is created automatically, since it is a child of the root
40+
h5io->dswrite(data, dataset_name);
41+
//! [tutorial_write_root_single_channel]
42+
43+
//! [tutorial_read_dataset]
44+
Mat expected;
45+
h5io->dsread(expected, dataset_name);
46+
//! [tutorial_read_dataset]
47+
48+
//! [tutorial_check_result]
49+
double diff = norm(data - expected);
50+
CV_Assert(abs(diff) < 1e-10);
51+
//! [tutorial_check_result]
52+
53+
h5io->close();
54+
}
55+
56+
void write_single_channel()
57+
{
58+
String filename = "single_channel.h5";
59+
String parent_name = "/data";
60+
String dataset_name = parent_name + "/single";
61+
62+
// prepare data
63+
Mat data;
64+
data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5);
65+
66+
Ptr<hdf::HDF5> h5io = hdf::open(filename);
67+
68+
//! [tutorial_create_dataset]
69+
// first we need to create the parent group
70+
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
71+
72+
// create the dataset if it not exists
73+
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
74+
//! [tutorial_create_dataset]
75+
76+
// the following is the same with the above function write_root_group_single_channel()
77+
78+
h5io->dswrite(data, dataset_name);
79+
80+
Mat expected;
81+
h5io->dsread(expected, dataset_name);
82+
83+
double diff = norm(data - expected);
84+
CV_Assert(abs(diff) < 1e-10);
85+
86+
h5io->close();
87+
}
88+
89+
/*
90+
* creating, reading and writing multiple-channel matrices
91+
* are the same with single channel matrices
92+
*/
93+
void write_multiple_channels()
94+
{
95+
String filename = "two_channels.h5";
96+
String parent_name = "/data";
97+
String dataset_name = parent_name + "/two_channels";
98+
99+
// prepare data
100+
Mat data(2, 3, CV_32SC2);
101+
for (size_t i = 0; i < data.total()*data.channels(); i++)
102+
((int*) data.data)[i] = (int)i;
103+
104+
Ptr<hdf::HDF5> h5io = hdf::open(filename);
105+
106+
// first we need to create the parent group
107+
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
108+
109+
// create the dataset if it not exists
110+
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
111+
112+
// the following is the same with the above function write_root_group_single_channel()
113+
114+
h5io->dswrite(data, dataset_name);
115+
116+
Mat expected;
117+
h5io->dsread(expected, dataset_name);
118+
119+
double diff = norm(data - expected);
120+
CV_Assert(abs(diff) < 1e-10);
121+
122+
h5io->close();
123+
}
124+
125+
int main()
126+
{
127+
write_root_group_single_channel();
128+
129+
write_single_channel();
130+
131+
write_multiple_channels();
132+
133+
return 0;
134+
}
135+
//! [tutorial]

0 commit comments

Comments
 (0)