filestorage.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * filestorage_sample demonstrate the usage of the opencv serialization functionality
  3. */
  4. #include "opencv2/core.hpp"
  5. #include <iostream>
  6. #include <string>
  7. using std::string;
  8. using std::cout;
  9. using std::endl;
  10. using std::cerr;
  11. using std::ostream;
  12. using namespace cv;
  13. static void help(char** av)
  14. {
  15. cout << "\nfilestorage_sample demonstrate the usage of the opencv serialization functionality.\n"
  16. << "usage:\n"
  17. << av[0] << " outputfile.yml.gz\n"
  18. << "\n outputfile above can have many different extensions, see below."
  19. << "\nThis program demonstrates the use of FileStorage for serialization, that is in use << and >> in OpenCV\n"
  20. << "For example, how to create a class and have it serialize, but also how to use it to read and write matrices.\n"
  21. << "FileStorage allows you to serialize to various formats specified by the file end type."
  22. << "\nYou should try using different file extensions.(e.g. yaml yml xml xml.gz yaml.gz etc...)\n" << endl;
  23. }
  24. struct MyData
  25. {
  26. MyData() :
  27. A(0), X(0), id()
  28. {
  29. }
  30. explicit MyData(int) :
  31. A(97), X(CV_PI), id("mydata1234")
  32. {
  33. }
  34. int A;
  35. double X;
  36. string id;
  37. void write(FileStorage& fs) const //Write serialization for this class
  38. {
  39. fs << "{" << "A" << A << "X" << X << "id" << id << "}";
  40. }
  41. void read(const FileNode& node) //Read serialization for this class
  42. {
  43. A = (int)node["A"];
  44. X = (double)node["X"];
  45. id = (string)node["id"];
  46. }
  47. };
  48. //These write and read functions must exist as per the inline functions in operations.hpp
  49. static void write(FileStorage& fs, const std::string&, const MyData& x){
  50. x.write(fs);
  51. }
  52. static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
  53. if(node.empty())
  54. x = default_value;
  55. else
  56. x.read(node);
  57. }
  58. static ostream& operator<<(ostream& out, const MyData& m){
  59. out << "{ id = " << m.id << ", ";
  60. out << "X = " << m.X << ", ";
  61. out << "A = " << m.A << "}";
  62. return out;
  63. }
  64. int main(int ac, char** av)
  65. {
  66. cv::CommandLineParser parser(ac, av,
  67. "{@input||}{help h ||}"
  68. );
  69. if (parser.has("help"))
  70. {
  71. help(av);
  72. return 0;
  73. }
  74. string filename = parser.get<string>("@input");
  75. if (filename.empty())
  76. {
  77. help(av);
  78. return 1;
  79. }
  80. //write
  81. {
  82. FileStorage fs(filename, FileStorage::WRITE);
  83. cout << "writing images\n";
  84. fs << "images" << "[";
  85. fs << "image1.jpg" << "myfi.png" << "baboon.jpg";
  86. cout << "image1.jpg" << " myfi.png" << " baboon.jpg" << endl;
  87. fs << "]";
  88. cout << "writing mats\n";
  89. Mat R =Mat_<double>::eye(3, 3),T = Mat_<double>::zeros(3, 1);
  90. cout << "R = " << R << "\n";
  91. cout << "T = " << T << "\n";
  92. fs << "R" << R;
  93. fs << "T" << T;
  94. cout << "writing MyData struct\n";
  95. MyData m(1);
  96. fs << "mdata" << m;
  97. cout << m << endl;
  98. }
  99. //read
  100. {
  101. FileStorage fs(filename, FileStorage::READ);
  102. if (!fs.isOpened())
  103. {
  104. cerr << "failed to open " << filename << endl;
  105. help(av);
  106. return 1;
  107. }
  108. FileNode n = fs["images"];
  109. if (n.type() != FileNode::SEQ)
  110. {
  111. cerr << "images is not a sequence! FAIL" << endl;
  112. return 1;
  113. }
  114. cout << "reading images\n";
  115. FileNodeIterator it = n.begin(), it_end = n.end();
  116. for (; it != it_end; ++it)
  117. {
  118. cout << (string)*it << "\n";
  119. }
  120. Mat R, T;
  121. cout << "reading R and T" << endl;
  122. fs["R"] >> R;
  123. fs["T"] >> T;
  124. cout << "R = " << R << "\n";
  125. cout << "T = " << T << endl;
  126. MyData m;
  127. fs["mdata"] >> m;
  128. cout << "read mdata\n";
  129. cout << m << endl;
  130. cout << "attempting to read mdata_b\n"; //Show default behavior for empty matrix
  131. fs["mdata_b"] >> m;
  132. cout << "read mdata_b\n";
  133. cout << m << endl;
  134. }
  135. cout << "Try opening " << filename << " to see the serialized data." << endl << endl;
  136. //read from string
  137. {
  138. cout << "Read data from string\n";
  139. string dataString =
  140. "%YAML:1.0\n"
  141. "mdata:\n"
  142. " A: 97\n"
  143. " X: 3.1415926535897931e+00\n"
  144. " id: mydata1234\n";
  145. MyData m;
  146. FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY);
  147. cout << "attempting to read mdata_b from string\n"; //Show default behavior for empty matrix
  148. fs["mdata"] >> m;
  149. cout << "read mdata\n";
  150. cout << m << endl;
  151. }
  152. //write to string
  153. {
  154. cout << "Write data to string\n";
  155. FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML);
  156. cout << "writing MyData struct\n";
  157. MyData m(1);
  158. fs << "mdata" << m;
  159. cout << m << endl;
  160. string createdString = fs.releaseAndGetString();
  161. cout << "Created string:\n" << createdString << "\n";
  162. }
  163. return 0;
  164. }