vendor: OpenCV 5.0.0 snapshot at 755e50675d97db9b7d449d8bd6b09888646f6c6e
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file create_groups.cpp
|
||||
* @author Fangjun Kuang <csukuangfj dot at gmail dot com>
|
||||
* @date December 2017
|
||||
*
|
||||
* @brief It demonstrates how to create HDF5 groups and subgroups.
|
||||
*
|
||||
* Basic steps:
|
||||
* 1. Use hdf::open to create a HDF5 file
|
||||
* 2. Use HDF5::hlexists to check if a group exists or not
|
||||
* 3. Use HDF5::grcreate to create a group by specifying its name
|
||||
* 4. Use hdf::close to close a HDF5 file after modifying it
|
||||
*
|
||||
*/
|
||||
|
||||
//! [tutorial]
|
||||
#include <iostream>
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/hdf.hpp>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
int main()
|
||||
{
|
||||
//! [create_group]
|
||||
|
||||
//! [tutorial_create_file]
|
||||
Ptr<hdf::HDF5> h5io = hdf::open("mytest.h5");
|
||||
//! [tutorial_create_file]
|
||||
|
||||
//! [tutorial_create_group]
|
||||
// "/" means the root group, which is always present
|
||||
if (!h5io->hlexists("/Group1"))
|
||||
h5io->grcreate("/Group1");
|
||||
else
|
||||
std::cout << "/Group1 has already been created, skip it.\n";
|
||||
//! [tutorial_create_group]
|
||||
|
||||
//! [tutorial_create_subgroup]
|
||||
// Note that Group1 has been created above, otherwise exception will occur
|
||||
if (!h5io->hlexists("/Group1/SubGroup1"))
|
||||
h5io->grcreate("/Group1/SubGroup1");
|
||||
else
|
||||
std::cout << "/Group1/SubGroup1 has already been created, skip it.\n";
|
||||
//! [tutorial_create_subgroup]
|
||||
|
||||
//! [tutorial_close_file]
|
||||
h5io->close();
|
||||
//! [tutorial_close_file]
|
||||
|
||||
//! [create_group]
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [tutorial]
|
||||
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* @file create_read_write_datasets.cpp
|
||||
* @author Fangjun Kuang <csukuangfj dot at gmail dot com>
|
||||
* @date December 2017
|
||||
*
|
||||
* @brief It demonstrates how to create a dataset, how
|
||||
* to write a cv::Mat to the dataset and how to
|
||||
* read a cv::Mat from it.
|
||||
*
|
||||
*/
|
||||
|
||||
//! [tutorial]
|
||||
#include <iostream>
|
||||
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/hdf.hpp>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
static void write_root_group_single_channel()
|
||||
{
|
||||
String filename = "root_group_single_channel.h5";
|
||||
String dataset_name = "/single"; // Note that it is a child of the root group /
|
||||
|
||||
// prepare data
|
||||
Mat data;
|
||||
data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5, 6);
|
||||
|
||||
//! [tutorial_open_file]
|
||||
Ptr<hdf::HDF5> h5io = hdf::open(filename);
|
||||
//! [tutorial_open_file]
|
||||
|
||||
//! [tutorial_write_root_single_channel]
|
||||
// write data to the given dataset
|
||||
// the dataset "/single" is created automatically, since it is a child of the root
|
||||
h5io->dswrite(data, dataset_name);
|
||||
//! [tutorial_write_root_single_channel]
|
||||
|
||||
//! [tutorial_read_dataset]
|
||||
Mat expected;
|
||||
h5io->dsread(expected, dataset_name);
|
||||
//! [tutorial_read_dataset]
|
||||
|
||||
//! [tutorial_check_result]
|
||||
double diff = norm(data - expected);
|
||||
CV_Assert(abs(diff) < 1e-10);
|
||||
//! [tutorial_check_result]
|
||||
|
||||
h5io->close();
|
||||
}
|
||||
|
||||
static void write_single_channel()
|
||||
{
|
||||
String filename = "single_channel.h5";
|
||||
String parent_name = "/data";
|
||||
String dataset_name = parent_name + "/single";
|
||||
|
||||
// prepare data
|
||||
Mat data;
|
||||
data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5);
|
||||
|
||||
Ptr<hdf::HDF5> h5io = hdf::open(filename);
|
||||
|
||||
//! [tutorial_create_dataset]
|
||||
// first we need to create the parent group
|
||||
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
|
||||
|
||||
// create the dataset if it not exists
|
||||
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
|
||||
//! [tutorial_create_dataset]
|
||||
|
||||
// the following is the same with the above function write_root_group_single_channel()
|
||||
|
||||
h5io->dswrite(data, dataset_name);
|
||||
|
||||
Mat expected;
|
||||
h5io->dsread(expected, dataset_name);
|
||||
|
||||
double diff = norm(data - expected);
|
||||
CV_Assert(abs(diff) < 1e-10);
|
||||
|
||||
h5io->close();
|
||||
}
|
||||
|
||||
/*
|
||||
* creating, reading and writing multiple-channel matrices
|
||||
* are the same with single channel matrices
|
||||
*/
|
||||
static void write_multiple_channels()
|
||||
{
|
||||
String filename = "two_channels.h5";
|
||||
String parent_name = "/data";
|
||||
String dataset_name = parent_name + "/two_channels";
|
||||
|
||||
// prepare data
|
||||
Mat data(2, 3, CV_32SC2);
|
||||
for (size_t i = 0; i < data.total()*data.channels(); i++)
|
||||
((int*) data.data)[i] = (int)i;
|
||||
|
||||
Ptr<hdf::HDF5> h5io = hdf::open(filename);
|
||||
|
||||
// first we need to create the parent group
|
||||
if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
|
||||
|
||||
// create the dataset if it not exists
|
||||
if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
|
||||
|
||||
// the following is the same with the above function write_root_group_single_channel()
|
||||
|
||||
h5io->dswrite(data, dataset_name);
|
||||
|
||||
Mat expected;
|
||||
h5io->dsread(expected, dataset_name);
|
||||
|
||||
double diff = norm(data - expected);
|
||||
CV_Assert(abs(diff) < 1e-10);
|
||||
|
||||
h5io->close();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
write_root_group_single_channel();
|
||||
|
||||
write_single_channel();
|
||||
|
||||
write_multiple_channels();
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [tutorial]
|
||||
@@ -0,0 +1,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]
|
||||
Reference in New Issue
Block a user