File: read_write_attributes.cpp

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 (93 lines) | stat: -rw-r--r-- 2,574 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
86
87
88
89
90
91
92
93
/**
 * @file read_write_attributes.cpp
 * @author Fangjun Kuang <csukuangfj dot at gmail dot com>
 * @date December 2017
 *
 * @brief It demonstrates how to read and write attributes inside the
 * root group.
 *
 * Currently, only the following datatypes can be used as attributes:
 *  - cv::String
 *  - int
 *  - double
 *  - cv::InputArray (n-d continuous multichannel arrays)
 *
 * Although HDF supports associating attributes with both datasets and groups,
 * only support for the root group is implemented by OpenCV at present.
 */

//! [tutorial]
#include <iostream>

#include <opencv2/core.hpp>
#include <opencv2/hdf.hpp>

using namespace cv;

static void read_write_attributes()
{
    String filename = "attributes.h5";

    //! [tutorial_open_file]
    Ptr<hdf::HDF5> h5io = hdf::open(filename);
    //! [tutorial_open_file]

    //! [tutorial_write_mat]
    String attr_mat_name = "array attribute";
    Mat attr_mat;
    attr_mat = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5, 6);
    if (!h5io->atexists(attr_mat_name))
        h5io->atwrite(attr_mat, attr_mat_name);
    //! [tutorial_write_mat]

    //! [snippets_write_str]
    String attr_str_name = "string attribute";
    String attr_str = "Hello HDF5 from OpenCV!";
    if (!h5io->atexists(attr_str_name))
        h5io->atwrite(attr_str, attr_str_name);
    //! [snippets_write_str]

    String attr_int_name = "int attribute";
    int attr_int = 123456;
    if (!h5io->atexists(attr_int_name))
        h5io->atwrite(attr_int, attr_int_name);

    String attr_double_name = "double attribute";
    double attr_double = 45678.123;
    if (!h5io->atexists(attr_double_name))
        h5io->atwrite(attr_double, attr_double_name);

    // read attributes
    Mat expected_attr_mat;
    int expected_attr_int;
    double expected_attr_double;

    //! [snippets_read_str]
    String expected_attr_str;
    h5io->atread(&expected_attr_str, attr_str_name);
    //! [snippets_read_str]

    //! [tutorial_read_mat]
    h5io->atread(expected_attr_mat, attr_mat_name);
    //! [tutorial_read_mat]
    h5io->atread(&expected_attr_int, attr_int_name);
    h5io->atread(&expected_attr_double, attr_double_name);

    // check results
    CV_Assert(norm(attr_mat - expected_attr_mat) < 1e-10);
    CV_Assert(attr_str.compare(expected_attr_str) == 0);
    CV_Assert(attr_int == expected_attr_int);
    CV_Assert(fabs(attr_double - expected_attr_double) < 1e-10);

    //! [tutorial_close_file]
    h5io->close();
    //! [tutorial_close_file]
}

int main()
{
    read_write_attributes();

    return 0;
}
//! [tutorial]