test_hdf5.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. /**
  5. * @file test_hdf5.cpp
  6. * @author Fangjun Kuang <csukuangfj dot at gmail dot com>
  7. * @date December 2017
  8. */
  9. #include "test_precomp.hpp"
  10. namespace opencv_test { namespace {
  11. struct HDF5_Test : public testing::Test
  12. {
  13. virtual void SetUp()
  14. {
  15. m_filename = "test.h5";
  16. // 0 1 2
  17. // 3 4 5
  18. m_single_channel.create(2, 3, CV_32F);
  19. for (size_t i = 0; i < m_single_channel.total(); i++)
  20. {
  21. ((float*)m_single_channel.data)[i] = i;
  22. }
  23. // 0 1 2 3 4 5
  24. // 6 7 8 9 10 11
  25. m_two_channels.create(2, 3, CV_32SC2);
  26. for (size_t i = 0; i < m_two_channels.total()*m_two_channels.channels(); i++)
  27. {
  28. ((int*)m_two_channels.data)[i] = (int)i;
  29. }
  30. }
  31. //! Remove the hdf5 file
  32. void reset()
  33. {
  34. remove(m_filename.c_str());
  35. }
  36. String m_filename; //!< filename for testing
  37. Ptr<hdf::HDF5> m_hdf_io; //!< HDF5 file pointer
  38. Mat m_single_channel; //!< single channel matrix for test
  39. Mat m_two_channels; //!< two-channel matrix for test
  40. };
  41. TEST_F(HDF5_Test, create_a_single_group)
  42. {
  43. reset();
  44. String group_name = "parent";
  45. m_hdf_io = hdf::open(m_filename);
  46. m_hdf_io->grcreate(group_name);
  47. EXPECT_EQ(m_hdf_io->hlexists(group_name), true);
  48. EXPECT_EQ(m_hdf_io->hlexists("child"), false);
  49. // It should fail since it creates a group with an existing name
  50. EXPECT_ANY_THROW(m_hdf_io->grcreate(group_name));
  51. m_hdf_io->close();
  52. }
  53. TEST_F(HDF5_Test, create_a_child_group)
  54. {
  55. reset();
  56. String parent = "parent";
  57. String child = parent + "/child";
  58. m_hdf_io = hdf::open(m_filename);
  59. m_hdf_io->grcreate(parent);
  60. m_hdf_io->grcreate(child);
  61. EXPECT_EQ(m_hdf_io->hlexists(parent), true);
  62. EXPECT_EQ(m_hdf_io->hlexists(child), true);
  63. m_hdf_io->close();
  64. }
  65. TEST_F(HDF5_Test, create_dataset)
  66. {
  67. reset();
  68. String dataset_single_channel = "/single";
  69. String dataset_two_channels = "/dual";
  70. m_hdf_io = hdf::open(m_filename);
  71. m_hdf_io->dscreate(m_single_channel.rows,
  72. m_single_channel.cols,
  73. m_single_channel.type(),
  74. dataset_single_channel);
  75. m_hdf_io->dscreate(m_two_channels.rows,
  76. m_two_channels.cols,
  77. m_two_channels.type(),
  78. dataset_two_channels);
  79. EXPECT_EQ(m_hdf_io->hlexists(dataset_single_channel), true);
  80. EXPECT_EQ(m_hdf_io->hlexists(dataset_two_channels), true);
  81. std::vector<int> dims;
  82. dims = m_hdf_io->dsgetsize(dataset_single_channel, hdf::HDF5::H5_GETDIMS);
  83. EXPECT_EQ(dims.size(), (size_t)2);
  84. EXPECT_EQ(dims[0], m_single_channel.rows);
  85. EXPECT_EQ(dims[1], m_single_channel.cols);
  86. dims = m_hdf_io->dsgetsize(dataset_two_channels, hdf::HDF5::H5_GETDIMS);
  87. EXPECT_EQ(dims.size(), (size_t)2);
  88. EXPECT_EQ(dims[0], m_two_channels.rows);
  89. EXPECT_EQ(dims[1], m_two_channels.cols);
  90. int type;
  91. type = m_hdf_io->dsgettype(dataset_single_channel);
  92. EXPECT_EQ(type, m_single_channel.type());
  93. type = m_hdf_io->dsgettype(dataset_two_channels);
  94. EXPECT_EQ(type, m_two_channels.type());
  95. m_hdf_io->close();
  96. }
  97. TEST_F(HDF5_Test, write_read_dataset_1)
  98. {
  99. reset();
  100. String dataset_single_channel = "/single";
  101. String dataset_two_channels = "/dual";
  102. m_hdf_io = hdf::open(m_filename);
  103. // since the dataset is under the root group, it is created by dswrite() automatically.
  104. m_hdf_io->dswrite(m_single_channel, dataset_single_channel);
  105. m_hdf_io->dswrite(m_two_channels, dataset_two_channels);
  106. EXPECT_EQ(m_hdf_io->hlexists(dataset_single_channel), true);
  107. EXPECT_EQ(m_hdf_io->hlexists(dataset_two_channels), true);
  108. // read single channel matrix
  109. Mat single;
  110. m_hdf_io->dsread(single, dataset_single_channel);
  111. EXPECT_EQ(single.type(), m_single_channel.type());
  112. EXPECT_EQ(single.size(), m_single_channel.size());
  113. EXPECT_LE(cvtest::norm(single, m_single_channel, NORM_L2), 1e-10);
  114. // read dual channel matrix
  115. Mat dual;
  116. m_hdf_io->dsread(dual, dataset_two_channels);
  117. EXPECT_EQ(dual.type(), m_two_channels.type());
  118. EXPECT_EQ(dual.size(), m_two_channels.size());
  119. EXPECT_LE(cvtest::norm(dual, m_two_channels, NORM_L2), 1e-10);
  120. m_hdf_io->close();
  121. }
  122. TEST_F(HDF5_Test, write_read_dataset_2)
  123. {
  124. reset();
  125. // create the dataset manually if it is not inside
  126. // the root group
  127. String parent = "/parent";
  128. String dataset_single_channel = parent + "/single";
  129. String dataset_two_channels = parent + "/dual";
  130. m_hdf_io = hdf::open(m_filename);
  131. m_hdf_io->grcreate(parent);
  132. EXPECT_EQ(m_hdf_io->hlexists(parent), true);
  133. m_hdf_io->dscreate(m_single_channel.rows,
  134. m_single_channel.cols,
  135. m_single_channel.type(),
  136. dataset_single_channel);
  137. m_hdf_io->dscreate(m_two_channels.rows,
  138. m_two_channels.cols,
  139. m_two_channels.type(),
  140. dataset_two_channels);
  141. EXPECT_EQ(m_hdf_io->hlexists(dataset_single_channel), true);
  142. EXPECT_EQ(m_hdf_io->hlexists(dataset_two_channels), true);
  143. m_hdf_io->dswrite(m_single_channel, dataset_single_channel);
  144. m_hdf_io->dswrite(m_two_channels, dataset_two_channels);
  145. EXPECT_EQ(m_hdf_io->hlexists(dataset_single_channel), true);
  146. EXPECT_EQ(m_hdf_io->hlexists(dataset_two_channels), true);
  147. // read single channel matrix
  148. Mat single;
  149. m_hdf_io->dsread(single, dataset_single_channel);
  150. EXPECT_EQ(single.type(), m_single_channel.type());
  151. EXPECT_EQ(single.size(), m_single_channel.size());
  152. EXPECT_LE(cvtest::norm(single, m_single_channel, NORM_L2), 1e-10);
  153. // read dual channel matrix
  154. Mat dual;
  155. m_hdf_io->dsread(dual, dataset_two_channels);
  156. EXPECT_EQ(dual.type(), m_two_channels.type());
  157. EXPECT_EQ(dual.size(), m_two_channels.size());
  158. EXPECT_LE(cvtest::norm(dual, m_two_channels, NORM_L2), 1e-10);
  159. m_hdf_io->close();
  160. }
  161. TEST_F(HDF5_Test, test_attribute)
  162. {
  163. reset();
  164. String attr_name = "test attribute name";
  165. int attr_value = 0x12345678;
  166. m_hdf_io = hdf::open(m_filename);
  167. EXPECT_EQ(m_hdf_io->atexists(attr_name), false);
  168. m_hdf_io->atwrite(attr_value, attr_name);
  169. EXPECT_ANY_THROW(m_hdf_io->atwrite(attr_value, attr_name)); // error! it already exists
  170. EXPECT_EQ(m_hdf_io->atexists(attr_name), true);
  171. int expected_attr_value;
  172. m_hdf_io->atread(&expected_attr_value, attr_name);
  173. EXPECT_EQ(attr_value, expected_attr_value);
  174. m_hdf_io->atdelete(attr_name);
  175. EXPECT_ANY_THROW(m_hdf_io->atdelete(attr_name)); // error! Delete non-existed attribute
  176. EXPECT_EQ(m_hdf_io->atexists(attr_name), false);
  177. m_hdf_io->close();
  178. }
  179. TEST_F(HDF5_Test, test_attribute_int)
  180. {
  181. reset();
  182. String attr_name = "test int";
  183. int attr_value = 0x12345678;
  184. m_hdf_io = hdf::open(m_filename);
  185. m_hdf_io->atwrite(attr_value, attr_name);
  186. int expected_attr_value;
  187. m_hdf_io->atread(&expected_attr_value, attr_name);
  188. EXPECT_EQ(attr_value, expected_attr_value);
  189. m_hdf_io->close();
  190. }
  191. TEST_F(HDF5_Test, test_attribute_double)
  192. {
  193. reset();
  194. String attr_name = "test double";
  195. double attr_value = 123.456789;
  196. m_hdf_io = hdf::open(m_filename);
  197. m_hdf_io->atwrite(attr_value, attr_name);
  198. double expected_attr_value;
  199. m_hdf_io->atread(&expected_attr_value, attr_name);
  200. EXPECT_NEAR(attr_value, expected_attr_value, 1e-9);
  201. m_hdf_io->close();
  202. }
  203. TEST_F(HDF5_Test, test_attribute_String)
  204. {
  205. reset();
  206. String attr_name = "test-String";
  207. String attr_value = "----_______----Hello HDF5----_______----\n";
  208. m_hdf_io = hdf::open(m_filename);
  209. m_hdf_io->atwrite(attr_value, attr_name);
  210. String got_attr_value;
  211. m_hdf_io->atread(&got_attr_value, attr_name);
  212. EXPECT_EQ(attr_value, got_attr_value);
  213. m_hdf_io->close();
  214. }
  215. TEST_F(HDF5_Test, test_attribute_String_empty)
  216. {
  217. reset();
  218. String attr_name = "test-empty-string";
  219. String attr_value;
  220. m_hdf_io = hdf::open(m_filename);
  221. m_hdf_io->atwrite(attr_value, attr_name);
  222. String got_attr_value;
  223. m_hdf_io->atread(&got_attr_value, attr_name);
  224. EXPECT_EQ(attr_value, got_attr_value);
  225. m_hdf_io->close();
  226. }
  227. TEST_F(HDF5_Test, test_attribute_InutArray_OutputArray_2d)
  228. {
  229. reset();
  230. String attr_name = "test-InputArray-OutputArray-2d";
  231. cv::Mat attr_value;
  232. std::vector<int> depth_vec;
  233. depth_vec.push_back(CV_8U); depth_vec.push_back(CV_8S);
  234. depth_vec.push_back(CV_16U); depth_vec.push_back(CV_16S);
  235. depth_vec.push_back(CV_32S); depth_vec.push_back(CV_32F);
  236. depth_vec.push_back(CV_64F);
  237. std::vector<int> channel_vec;
  238. channel_vec.push_back(1); channel_vec.push_back(2);
  239. channel_vec.push_back(3); channel_vec.push_back(4);
  240. channel_vec.push_back(5); channel_vec.push_back(6);
  241. channel_vec.push_back(7); channel_vec.push_back(8);
  242. channel_vec.push_back(9); channel_vec.push_back(10);
  243. std::vector<std::vector<int> > dim_vec;
  244. std::vector<int> dim_2d;
  245. dim_2d.push_back(2); dim_2d.push_back(3);
  246. dim_vec.push_back(dim_2d);
  247. std::vector<int> dim_3d;
  248. dim_3d.push_back(2);
  249. dim_3d.push_back(3);
  250. dim_3d.push_back(4);
  251. dim_vec.push_back(dim_3d);
  252. std::vector<int> dim_4d;
  253. dim_4d.push_back(2); dim_4d.push_back(3);
  254. dim_4d.push_back(4); dim_4d.push_back(5);
  255. dim_vec.push_back(dim_4d);
  256. Mat expected_attr_value;
  257. m_hdf_io = hdf::open(m_filename);
  258. for (size_t i = 0; i < depth_vec.size(); i++)
  259. for (size_t j = 0; j < channel_vec.size(); j++)
  260. for (size_t k = 0; k < dim_vec.size(); k++)
  261. {
  262. if (m_hdf_io->atexists(attr_name))
  263. m_hdf_io->atdelete(attr_name);
  264. attr_value.create(dim_vec[k], CV_MAKETYPE(depth_vec[i], channel_vec[j]));
  265. randu(attr_value, 0, 255);
  266. m_hdf_io->atwrite(attr_value, attr_name);
  267. m_hdf_io->atread(expected_attr_value, attr_name);
  268. double diff = cvtest::norm(attr_value, expected_attr_value, NORM_L2);
  269. EXPECT_LE(diff, 1e-6);
  270. EXPECT_EQ(attr_value.size, expected_attr_value.size);
  271. EXPECT_EQ(attr_value.type(), expected_attr_value.type());
  272. }
  273. m_hdf_io->close();
  274. }
  275. }} // namespace