create_read_write_datasets.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file create_read_write_datasets.cpp
  3. * @author Fangjun Kuang <csukuangfj dot at gmail dot com>
  4. * @date December 2017
  5. *
  6. * @brief It demonstrates how to create a dataset, how
  7. * to write a cv::Mat to the dataset and how to
  8. * read a cv::Mat from it.
  9. *
  10. */
  11. //! [tutorial]
  12. #include <iostream>
  13. #include <opencv2/core.hpp>
  14. #include <opencv2/hdf.hpp>
  15. using namespace cv;
  16. static void write_root_group_single_channel()
  17. {
  18. String filename = "root_group_single_channel.h5";
  19. String dataset_name = "/single"; // Note that it is a child of the root group /
  20. // prepare data
  21. Mat data;
  22. data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5, 6);
  23. //! [tutorial_open_file]
  24. Ptr<hdf::HDF5> h5io = hdf::open(filename);
  25. //! [tutorial_open_file]
  26. //! [tutorial_write_root_single_channel]
  27. // write data to the given dataset
  28. // the dataset "/single" is created automatically, since it is a child of the root
  29. h5io->dswrite(data, dataset_name);
  30. //! [tutorial_write_root_single_channel]
  31. //! [tutorial_read_dataset]
  32. Mat expected;
  33. h5io->dsread(expected, dataset_name);
  34. //! [tutorial_read_dataset]
  35. //! [tutorial_check_result]
  36. double diff = norm(data - expected);
  37. CV_Assert(abs(diff) < 1e-10);
  38. //! [tutorial_check_result]
  39. h5io->close();
  40. }
  41. static void write_single_channel()
  42. {
  43. String filename = "single_channel.h5";
  44. String parent_name = "/data";
  45. String dataset_name = parent_name + "/single";
  46. // prepare data
  47. Mat data;
  48. data = (cv::Mat_<float>(2, 3) << 0, 1, 2, 3, 4, 5);
  49. Ptr<hdf::HDF5> h5io = hdf::open(filename);
  50. //! [tutorial_create_dataset]
  51. // first we need to create the parent group
  52. if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
  53. // create the dataset if it not exists
  54. if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
  55. //! [tutorial_create_dataset]
  56. // the following is the same with the above function write_root_group_single_channel()
  57. h5io->dswrite(data, dataset_name);
  58. Mat expected;
  59. h5io->dsread(expected, dataset_name);
  60. double diff = norm(data - expected);
  61. CV_Assert(abs(diff) < 1e-10);
  62. h5io->close();
  63. }
  64. /*
  65. * creating, reading and writing multiple-channel matrices
  66. * are the same with single channel matrices
  67. */
  68. static void write_multiple_channels()
  69. {
  70. String filename = "two_channels.h5";
  71. String parent_name = "/data";
  72. String dataset_name = parent_name + "/two_channels";
  73. // prepare data
  74. Mat data(2, 3, CV_32SC2);
  75. for (size_t i = 0; i < data.total()*data.channels(); i++)
  76. ((int*) data.data)[i] = (int)i;
  77. Ptr<hdf::HDF5> h5io = hdf::open(filename);
  78. // first we need to create the parent group
  79. if (!h5io->hlexists(parent_name)) h5io->grcreate(parent_name);
  80. // create the dataset if it not exists
  81. if (!h5io->hlexists(dataset_name)) h5io->dscreate(data.rows, data.cols, data.type(), dataset_name);
  82. // the following is the same with the above function write_root_group_single_channel()
  83. h5io->dswrite(data, dataset_name);
  84. Mat expected;
  85. h5io->dsread(expected, dataset_name);
  86. double diff = norm(data - expected);
  87. CV_Assert(abs(diff) < 1e-10);
  88. h5io->close();
  89. }
  90. int main()
  91. {
  92. write_root_group_single_channel();
  93. write_single_channel();
  94. write_multiple_channels();
  95. return 0;
  96. }
  97. //! [tutorial]