Skip to content

Commit f03e415

Browse files
committed
Merge pull request #1493 from csukuangfj:improve-hdf
2 parents 81ca8da + 13508c7 commit f03e415

File tree

16 files changed

+809
-174
lines changed

16 files changed

+809
-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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)