OpenCV 5.0.0
Open Source Computer Vision
読み込み中...
検索中...
見つかりません
🤖 AIによる機械翻訳(非公式) — これは OpenCV 5.0.0 公式リファレンス(英語)を AI (Claude) で自動翻訳したものです。訳に誤りを含む場合があります。正確な情報は 公式英語版(原文) を参照してください。
データセットの作成・書き込み・読み込み

目標

このチュートリアルでは次の内容を示す:

  • データセットを作成する方法は?
  • データセットに cv::Mat を書き込むには?
  • データセットから cv::Mat を読み込むには?
覚え書き
現在のところ、cv::Mat の読み書きのみをサポートしており、行列はメモリ上で連続している必要がある。他のデータ型のサポートはまだ実装されていない。

ソースコード

以下のコードは、シングルチャンネル行列と2チャンネル行列をデータセットに書き込み、それらを読み戻す方法を示している。

コードは こちら からダウンロードするか、opencv_contrib のソースコードライブラリの modules/hdf/samples/create_read_write_datasets.cpp ファイルから見つけることができる。

#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);
Ptr<hdf::HDF5> h5io = hdf::open(filename);
// 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);
Mat expected;
h5io->dsread(expected, dataset_name);
double diff = norm(data - expected);
CV_Assert(abs(diff) < 1e-10);
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);
// 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();
}
/*
* 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;
}
#define CV_32SC2
Definition interface.h:102

解説

データセットを作成する最初のステップは、ファイルを開くことである

Ptr<hdf::HDF5> h5io = hdf::open(filename);

関数 write_root_group_single_channel() では、データセット名が /single であり、これはルートグループの内部にあるため、次のように記述できる

// 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);

これにより、事前にデータセットを作成する必要なく、データを直接データセットに書き込むことができる。なぜなら、cv::hdf::HDF5::dswrite() の内部で自動的に作成されるからである。

警告
これはルートグループの内部に存在するデータセットにのみ適用される。

もちろん、自分でデータセットを作成することもできる:

// 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);

データセットからデータを読み込むには、次を使用する

Mat expected;
h5io->dsread(expected, dataset_name);

データセットの名前を指定することによって。

次を使用することで、読み出したデータが以前に書き込んだデータと完全に一致することを確認できる

double diff = norm(data - expected);
CV_Assert(abs(diff) < 1e-10);

結果

図1は、ファイル root_group_single_channel についてツール HDFView を使用して可視化した結果を示している。ルートグループの直接の子ではないデータセットの行列の結果は、それぞれ図2と図3に示している。

Figure 1: Result for writing a single channel matrix to a dataset inside the root group
Figure 2: Result for writing a single channel matrix to a dataset not in the root group
Figure 3: Result for writing a two-channel matrix to a dataset not in the root group