create_groups.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @file create_groups.cpp
  3. * @author Fangjun Kuang <csukuangfj dot at gmail dot com>
  4. * @date December 2017
  5. *
  6. * @brief It demonstrates how to create HDF5 groups and subgroups.
  7. *
  8. * Basic steps:
  9. * 1. Use hdf::open to create a HDF5 file
  10. * 2. Use HDF5::hlexists to check if a group exists or not
  11. * 3. Use HDF5::grcreate to create a group by specifying its name
  12. * 4. Use hdf::close to close a HDF5 file after modifying it
  13. *
  14. */
  15. //! [tutorial]
  16. #include <iostream>
  17. #include <opencv2/core.hpp>
  18. #include <opencv2/hdf.hpp>
  19. using namespace cv;
  20. int main()
  21. {
  22. //! [create_group]
  23. //! [tutorial_create_file]
  24. Ptr<hdf::HDF5> h5io = hdf::open("mytest.h5");
  25. //! [tutorial_create_file]
  26. //! [tutorial_create_group]
  27. // "/" means the root group, which is always present
  28. if (!h5io->hlexists("/Group1"))
  29. h5io->grcreate("/Group1");
  30. else
  31. std::cout << "/Group1 has already been created, skip it.\n";
  32. //! [tutorial_create_group]
  33. //! [tutorial_create_subgroup]
  34. // Note that Group1 has been created above, otherwise exception will occur
  35. if (!h5io->hlexists("/Group1/SubGroup1"))
  36. h5io->grcreate("/Group1/SubGroup1");
  37. else
  38. std::cout << "/Group1/SubGroup1 has already been created, skip it.\n";
  39. //! [tutorial_create_subgroup]
  40. //! [tutorial_close_file]
  41. h5io->close();
  42. //! [tutorial_close_file]
  43. //! [create_group]
  44. return 0;
  45. }
  46. //! [tutorial]