opencv xml读写

前端之家收集整理的这篇文章主要介绍了opencv xml读写前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #include <opencv2/core/core.hpp>
  2. #include <iostream>
  3. #include <string>
  4. #include <time.h>
  5.  
  6. using namespace cv;
  7. using namespace std;
  8.  
  9.  
  10. int main(int argc,char** argv)
  11. {
  12.  
  13. if (0)//write
  14. {
  15. FileStorage fs("test.xml",FileStorage::WRITE);
  16.  
  17. fs << "frameCount" << 5;
  18. time_t rawtime; time(&rawtime);
  19. fs << "calibrationDate" << asctime(localtime(&rawtime));
  20. Mat cameraMatrix = (Mat_<double>(3,3) << 1000,320,1000,240,1);
  21. Mat distCoeffs = (Mat_<double>(5,1) << 0.1,0.01,-0.001,0);
  22. fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
  23. fs << "features" << "[";
  24. for( int i = 0; i < 3; i++ )
  25. {
  26. int x = rand() % 640;
  27. int y = rand() % 480;
  28. uchar lbp = rand() % 256;
  29.  
  30. fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
  31. for( int j = 0; j < 8; j++ )
  32. fs << ((lbp >> j) & 1);
  33. fs << "]" << "}";
  34. }
  35. fs << "]";
  36. fs.release();
  37. }
  38. else
  39. {
  40. FileStorage fs2("test.xml",FileStorage::READ);
  41.  
  42. // first method: use (type) operator on FileNode.
  43. int frameCount = (int)fs2["frameCount"];
  44.  
  45. std::string date;
  46. // second method: use FileNode::operator >>
  47. fs2["calibrationDate"] >> date;
  48.  
  49. Mat cameraMatrix2,distCoeffs2;
  50. fs2["cameraMatrix"] >> cameraMatrix2;
  51. fs2["distCoeffs"] >> distCoeffs2;
  52.  
  53. cout << "frameCount: " << frameCount << endl
  54. << "calibration date: " << date << endl
  55. << "camera matrix: " << cameraMatrix2 << endl
  56. << "distortion coeffs: " << distCoeffs2 << endl;
  57.  
  58. FileNode features = fs2["features"];
  59. FileNodeIterator it = features.begin(),it_end = features.end();
  60. int idx = 0;
  61. std::vector<uchar> lbpval;
  62.  
  63. // iterate through a sequence using FileNodeIterator
  64. for( ; it != it_end; ++it,idx++ )
  65. {
  66. cout << "feature #" << idx << ": ";
  67. cout << "x=" << (int)(*it)["x"] << ",y=" << (int)(*it)["y"] << ",lbp: (";
  68. // you can also easily read numerical arrays using FileNode >> std::vector operator.
  69. (*it)["lbp"] >> lbpval;
  70. for( int i = 0; i < (int)lbpval.size(); i++ )
  71. cout << " " << (int)lbpval[i];
  72. cout << ")" << endl;
  73. }
  74. fs2.release();
  75. }
  76. return 0;
  77. }

猜你在找的XML相关文章