File: read_write_attributes.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 (57 lines) | stat: -rw-r--r-- 1,808 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
Reading and Writing Attributes{#tutorial_hdf_read_write_attributes}
===============================

Goal
----
This tutorial shows you:
 - How to write attributes?
 - How to read attributes?

@note Although attributes can be associated with groups and datasets, only attributes
with the root group are implemented in OpenCV. Supported attribute types are
`int`, `double`, `cv::String` and `cv::InputArray` (only for continuous arrays).

Source Code
----

The following code demonstrates reading and writing attributes
inside the root group with data types `cv::Mat`, `cv::String`, `int`
and `double`.

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

@snippet samples/read_write_attributes.cpp tutorial

Explanation
----

The first step is to open the HDF5 file:

@snippet samples/read_write_attributes.cpp tutorial_open_file

Then we use cv::hdf::HDF5::atwrite() to write attributes by specifying its value and name:

@snippet samples/read_write_attributes.cpp tutorial_write_mat

@warning Before writing an attribute, we have to make sure that
the attribute does not exist using cv::hdf::HDF5::atexists().

To read an attribute, we use cv::hdf::HDF5::atread() by specifying the attribute name

@snippet samples/read_write_attributes.cpp tutorial_read_mat

In the end, we have to close the HDF file

@snippet samples/read_write_attributes.cpp tutorial_close_file

Results
----

Figure 1 and Figure 2 give the results visualized using the tool HDFView.

![Figure 1: Attributes of the root group](pics/attributes-file.png)

![Figure 2: Detailed attribute information](pics/attributes-details.png)

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