File: how_to_create_groups.markdown

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (85 lines) | stat: -rw-r--r-- 2,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Creating Groups {#tutorial_hdf_create_groups}
===============================

Goal
----

This tutorial will show you:
 - How to create a HDF5 file?
 - How to create a group?
 - How to check whether a given group exists or not?
 - How to create a subgroup?

Source Code
----

The following code creates two groups: `Group1` and `SubGroup1`, where
`SubGroup1` is a child of `Group1`.

You can download the code from [here][1] or find it in the file
`modules/hdf/samples/create_groups.cpp` of the opencv_contrib source code library.

@snippet samples/create_groups.cpp tutorial

Explanation
----

First, we create a HDF5 file

@snippet samples/create_groups.cpp tutorial_create_file

If the given file does not exist, it will be created. Otherwise, it is open for read and write.

Next, we create the group `Group1`

@snippet samples/create_groups.cpp tutorial_create_group

Note that we have to check whether `/Group1` exists or not using
the function cv::hdf::HDF5::hlexists() before creating it. You can not create
a group with an existing name. Otherwise, an error will occur.

Then, we create the subgroup named `Subgroup1`. In order to
indicate that it is a sub group of `Group1`, we have to
use the group name `/Group1/SubGroup1`:

@snippet samples/create_groups.cpp tutorial_create_subgroup

Note that before creating a subgroup, we have to make sure
that its parent group exists. Otherwise, an error will occur.

In the end, we have to close the file

@snippet samples/create_groups.cpp tutorial_close_file

Result
----

There are many tools that can be used to inspect a given HDF file, such
as HDFView and h5dump. If you are using Ubuntu, you can install
them with the following commands:

@code
sudo apt-get install hdf5-tools hdfview
@endcode

There are also binaries available from the The HDF Group official website <https://support.hdfgroup.org/HDF5/Tutor/tools.html>.

The following figure shows the result visualized with the tool HDFView:

![Figure 1: Results of creating groups and subgroups](pics/create_groups.png)

The output for `h5dump` is:

@code
$ h5dump mytest.h5
HDF5 "mytest.h5" {
GROUP "/" {
   GROUP "Group1" {
      GROUP "SubGroup1" {
      }
   }
}
}
@endcode

[1]: https://github.com/opencv/opencv_contrib/tree/master/modules/hdf/samples/create_groups.cpp