Advertisement
bkit4s0

[write & read xml file using opencv]

May 20th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <opencv2\core\core.hpp>
  2. #include <opencv2\highgui\highgui.hpp>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace cv;
  7. using namespace std;
  8.  
  9. int main(int ac, char** av)
  10. {
  11.     string filename = av[1];
  12.     {
  13.         Mat  R = Mat_<uchar>::eye(3,3),
  14.             T = Mat_<double>::zeros(3,1);
  15.         int sz[3] = {2,2,2};
  16.         Mat L(3,sz, CV_8UC(1), Scalar::all(0));
  17.         Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
  18.         Mat M = imread("image.jpg");
  19.  
  20.         FileStorage fs(filename, FileStorage::WRITE);
  21.         fs << "iterationNr" << 100;
  22.         fs << "strings" << "[";
  23.         fs << "image1.jpg" << "Awasomeness" << "baboon.jpg";
  24.         fs << "]";
  25.  
  26.         fs << "Mapping";
  27.         fs << "{" << "One" << 1;
  28.         fs << "Two" << 2 << "}";
  29.  
  30.         fs << "R" << R;
  31.         fs << "T" << T;
  32.         fs << "C" << C;
  33.         fs << "M" << M;
  34.  
  35.         fs.release();
  36.         cout << "Write Done." << endl;
  37.     }
  38.  
  39.     { // read
  40.         cout << endl << "Reading: "  << endl;
  41.         FileStorage fs;
  42.         fs.open(filename, FileStorage::READ);
  43.  
  44.         int itNr;
  45.         itNr = fs["iterationNr"];
  46.         cout << itNr << endl;
  47.  
  48.         if( !fs.isOpened() )
  49.         {
  50.             cerr << "Failed to open" << filename << endl;
  51.             return 1;
  52.         }
  53.  
  54.         FileNode n = fs["strings"];
  55.         if (n.type() != FileNode::SEQ)
  56.         {
  57.             cerr << "strings is not a sequence ! FAIL";
  58.         }
  59.         FileNodeIterator it = n.begin(), it_end = n.end();
  60.         for (;it != it_end;++it)
  61.             cout << (string)*it << endl;
  62.        
  63.         n = fs["Mapping"];
  64.         cout<< "Two " << (int)(n["Two"]) << "; ";
  65.         cout<< "One " << (int)(n["One"]) << endl << endl;
  66.  
  67.         Mat M;
  68.         fs["M"] >> M;
  69.         cout << endl << "M =" << M << endl;
  70.     }
  71.     getchar();
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement