Skip to content

Commit 31bd7d9

Browse files
committed
Merge pull request #1500 from csukuangfj:hdf-attributes-support
2 parents 43143d6 + 7f7b0bc commit 31bd7d9

File tree

12 files changed

+654
-40
lines changed

12 files changed

+654
-40
lines changed
24.2 KB
Loading
21 KB
Loading

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

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,109 @@ class CV_EXPORTS_W HDF5
103103
*/
104104
CV_WRAP virtual bool hlexists( const String& label ) const = 0;
105105

106-
/* @overload */
106+
/**
107+
* Check whether a given attribute exits or not in the root group.
108+
*
109+
* @param atlabel the attribute name to be checked.
110+
* @return true if the attribute exists, false otherwise.
111+
*
112+
* @sa atdelete, atwrite, atread
113+
*/
114+
CV_WRAP virtual bool atexists(const String& atlabel) const = 0;
115+
116+
/**
117+
* Delete an attribute from the root group.
118+
*
119+
* @param atlabel the attribute to be deleted.
120+
*
121+
* @note CV_Error() is called if the given attribute does not exist. Use atexists()
122+
* to check whether it exists or not beforehand.
123+
*
124+
* @sa atexists, atwrite, atread
125+
*/
126+
CV_WRAP virtual void atdelete(const String& atlabel) = 0;
127+
128+
/**
129+
* Write an attribute inside the root group.
130+
*
131+
* @param value attribute value.
132+
* @param atlabel attribute name.
133+
*
134+
* The following example demonstrates how to write an attribute of type cv::String:
135+
*
136+
* @snippet samples/read_write_attributes.cpp snippets_write_str
137+
*
138+
* @note CV_Error() is called if the given attribute already exists. Use atexists()
139+
* to check whether it exists or not beforehand. And use atdelete() to delete
140+
* it if it already exists.
141+
*
142+
* @sa atexists, atdelete, atread
143+
*/
144+
CV_WRAP virtual void atwrite(const int value, const String& atlabel) = 0;
145+
146+
/**
147+
* Read an attribute from the root group.
148+
*
149+
* @param value address where the attribute is read into
150+
* @param atlabel attribute name
151+
*
152+
* The following example demonstrates how to read an attribute of type cv::String:
153+
*
154+
* @snippet samples/read_write_attributes.cpp snippets_read_str
155+
*
156+
* @note The attribute MUST exist, otherwise CV_Error() is called. Use atexists()
157+
* to check if it exists beforehand.
158+
*
159+
* @sa atexists, atdelete, atwrite
160+
*/
161+
CV_WRAP virtual void atread(int* value, const String& atlabel) = 0;
162+
163+
/** @overload */
164+
CV_WRAP virtual void atwrite(const double value, const String& atlabel) = 0;
165+
166+
/** @overload */
167+
CV_WRAP virtual void atread(double* value, const String& atlabel) = 0;
168+
169+
/** @overload */
170+
CV_WRAP virtual void atwrite(const String& value, const String& atlabel) = 0;
171+
172+
/** @overload */
173+
CV_WRAP virtual void atread(String* value, const String& atlabel) = 0;
174+
175+
/**
176+
* Write an attribute into the root group.
177+
*
178+
* @param value attribute value. Currently, only n-d continuous multi-channel arrays are supported.
179+
* @param atlabel attribute name.
180+
*
181+
* @note CV_Error() is called if the given attribute already exists. Use atexists()
182+
* to check whether it exists or not beforehand. And use atdelete() to delete
183+
* it if it already exists.
184+
*
185+
* @sa atexists, atdelete, atread.
186+
*/
187+
CV_WRAP virtual void atwrite(InputArray value, const String& atlabel) = 0;
188+
189+
/**
190+
* Read an attribute from the root group.
191+
*
192+
* @param value attribute value. Currently, only n-d continuous multi-channel arrays are supported.
193+
* @param atlabel attribute name.
194+
*
195+
* @note The attribute MUST exist, otherwise CV_Error() is called. Use atexists()
196+
* to check if it exists beforehand.
197+
*
198+
* @sa atexists, atdelete, atwrite
199+
*/
200+
CV_WRAP virtual void atread(OutputArray value, const String& atlabel) = 0;
201+
202+
/** @overload */
107203
CV_WRAP virtual void dscreate( const int rows, const int cols, const int type,
108204
const String& dslabel ) const = 0;
109-
/* @overload */
205+
/** @overload */
110206
CV_WRAP virtual void dscreate( const int rows, const int cols, const int type,
111207
const String& dslabel, const int compresslevel ) const = 0;
112-
/* @overload */
208+
/** @overload */
113209
CV_WRAP virtual void dscreate( const int rows, const int cols, const int type,
114210
const String& dslabel, const int compresslevel, const vector<int>& dims_chunks ) const = 0;
115211
/** @brief Create and allocate storage for two dimensional single or multi channel dataset.

modules/hdf/samples/create_groups.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
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-
51
/**
62
* @file create_groups.cpp
73
* @author Fangjun Kuang <csukuangfj dot at gmail dot com>

modules/hdf/samples/create_read_write_datasets.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
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-
51
/**
6-
* @file create_read_write.cpp
2+
* @file create_read_write_datasets.cpp
73
* @author Fangjun Kuang <csukuangfj dot at gmail dot com>
84
* @date December 2017
95
*
@@ -13,14 +9,6 @@
139
*
1410
*/
1511

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-
2412
//! [tutorial]
2513
#include <iostream>
2614

@@ -29,7 +17,7 @@
2917

3018
using namespace cv;
3119

32-
void write_root_group_single_channel()
20+
static void write_root_group_single_channel()
3321
{
3422
String filename = "root_group_single_channel.h5";
3523
String dataset_name = "/single"; // Note that it is a child of the root group /
@@ -61,7 +49,7 @@ void write_root_group_single_channel()
6149
h5io->close();
6250
}
6351

64-
void write_single_channel()
52+
static void write_single_channel()
6553
{
6654
String filename = "single_channel.h5";
6755
String parent_name = "/data";
@@ -98,7 +86,7 @@ void write_single_channel()
9886
* creating, reading and writing multiple-channel matrices
9987
* are the same with single channel matrices
10088
*/
101-
void write_multiple_channels()
89+
static void write_multiple_channels()
10290
{
10391
String filename = "two_channels.h5";
10492
String parent_name = "/data";
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @file read_write_attributes.cpp
3+
* @author Fangjun Kuang <csukuangfj dot at gmail dot com>
4+
* @date December 2017
5+
*
6+
* @brief It demonstrates how to read and write attributes inside the
7+
* root group.
8+
*
9+
* Currently, only the following datatypes can be used as attributes:
10+
* - cv::String
11+
* - int
12+
* - double
13+
* - cv::InputArray (n-d continuous multichannel arrays)
14+
*
15+
* Although HDF supports associating attributes with both datasets and groups,
16+
* only support for the root group is implemented by OpenCV at present.
17+
*/
18+
19+
//! [tutorial]
20+
#include <iostream>
21+
22+
#include <opencv2/core.hpp>
23+
#include <opencv2/hdf.hpp>
24+
25+
using namespace cv;
26+
27+
static void read_write_attributes()
28+
{
29+
String filename = "attributes.h5";
30+
31+
//! [tutorial_open_file]
32+
Ptr<hdf::HDF5> h5io = hdf::open(filename);
33+
//! [tutorial_open_file]
34+
35+
//! [tutorial_write_mat]
36+
String attr_mat_name = "array attribute";
37+
Mat attr_mat;
38+
attr_mat = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5, 6);
39+
if (!h5io->atexists(attr_mat_name))
40+
h5io->atwrite(attr_mat, attr_mat_name);
41+
//! [tutorial_write_mat]
42+
43+
//! [snippets_write_str]
44+
String attr_str_name = "string attribute";
45+
String attr_str = "Hello HDF5 from OpenCV!";
46+
if (!h5io->atexists(attr_str_name))
47+
h5io->atwrite(attr_str, attr_str_name);
48+
//! [snippets_write_str]
49+
50+
String attr_int_name = "int attribute";
51+
int attr_int = 123456;
52+
if (!h5io->atexists(attr_int_name))
53+
h5io->atwrite(attr_int, attr_int_name);
54+
55+
String attr_double_name = "double attribute";
56+
double attr_double = 45678.123;
57+
if (!h5io->atexists(attr_double_name))
58+
h5io->atwrite(attr_double, attr_double_name);
59+
60+
// read attributes
61+
Mat expected_attr_mat;
62+
int expected_attr_int;
63+
double expected_attr_double;
64+
65+
//! [snippets_read_str]
66+
String expected_attr_str;
67+
h5io->atread(&expected_attr_str, attr_str_name);
68+
//! [snippets_read_str]
69+
70+
//! [tutorial_read_mat]
71+
h5io->atread(expected_attr_mat, attr_mat_name);
72+
//! [tutorial_read_mat]
73+
h5io->atread(&expected_attr_int, attr_int_name);
74+
h5io->atread(&expected_attr_double, attr_double_name);
75+
76+
// check results
77+
CV_Assert(norm(attr_mat - expected_attr_mat) < 1e-10);
78+
CV_Assert(attr_str.compare(expected_attr_str) == 0);
79+
CV_Assert(attr_int == expected_attr_int);
80+
CV_Assert(fabs(attr_double - expected_attr_double) < 1e-10);
81+
82+
//! [tutorial_close_file]
83+
h5io->close();
84+
//! [tutorial_close_file]
85+
}
86+
87+
int main()
88+
{
89+
read_write_attributes();
90+
91+
return 0;
92+
}
93+
//! [tutorial]

0 commit comments

Comments
 (0)