filestorage.cpp 4.63 KB
Newer Older
1 2 3 4
/*
 * filestorage_sample demonstrate the usage of the opencv serialization functionality
 */

5
#include "opencv2/core.hpp"
6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::ostream;
using namespace cv;

16
static void help(char** av)
17
{
Gary Bradski's avatar
Gary Bradski committed
18
  cout << "\nfilestorage_sample demonstrate the usage of the opencv serialization functionality.\n"
19 20 21 22 23 24
      << "usage:\n"
      <<  av[0] << " outputfile.yml.gz\n"
      << "\n   outputfile above can have many different extenstions, see below."
      << "\nThis program demonstrates the use of FileStorage for serialization, that is use << and >>  in OpenCV\n"
      << "For example, how to create a class and have it serialize, but also how to use it to read and write matrices.\n"
      << "FileStorage allows you to serialize to various formats specified by the file end type."
Gary Bradski's avatar
Gary Bradski committed
25
          << "\nYou should try using different file extensions.(e.g. yaml yml xml xml.gz yaml.gz etc...)\n" << endl;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
}

struct MyData
{
  MyData() :
    A(0), X(0), id()
  {
  }
  explicit MyData(int) :
    A(97), X(CV_PI), id("mydata1234")
  {
  }
  int A;
  double X;
  string id;
Gary Bradski's avatar
Gary Bradski committed
41
  void write(FileStorage& fs) const //Write serialization for this class
42 43 44
  {
    fs << "{" << "A" << A << "X" << X << "id" << id << "}";
  }
Gary Bradski's avatar
Gary Bradski committed
45
  void read(const FileNode& node)  //Read serialization for this class
46 47 48 49 50 51 52 53
  {

    A = (int)node["A"];
    X = (double)node["X"];
    id = (string)node["id"];
  }
};

Gary Bradski's avatar
Gary Bradski committed
54
//These write and read functions must exist as per the inline functions in operations.hpp
55
static void write(FileStorage& fs, const std::string&, const MyData& x){
56 57
  x.write(fs);
}
58
static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
59 60 61
  if(node.empty())
    x = default_value;
  else
62
    x.read(node);
63 64
}

65
static ostream& operator<<(ostream& out, const MyData& m){
66 67 68 69 70 71 72
  out << "{ id = " << m.id << ", ";
  out << "X = " << m.X << ", ";
  out << "A = " << m.A << "}";
  return out;
}
int main(int ac, char** av)
{
ValeryTyumen's avatar
ValeryTyumen committed
73 74 75 76 77 78 79 80 81 82
  cv::CommandLineParser parser(ac, av,
    "{@input||}{help h ||}"
  );
  if (parser.has("help"))
  {
    help(av);
    return 0;
  }
  string filename = parser.get<string>("@input");
  if (filename.empty())
83 84 85 86 87 88 89 90 91 92 93 94
  {
    help(av);
    return 1;
  }

  //write
  {
    FileStorage fs(filename, FileStorage::WRITE);

    cout << "writing images\n";
    fs << "images" << "[";

Dmitriy Anisimov's avatar
Dmitriy Anisimov committed
95 96
    fs << "image1.jpg" << "myfi.png" << "../data/baboon.jpg";
    cout << "image1.jpg" << " myfi.png" << " ../data/baboon.jpg" << endl;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

    fs << "]";

    cout << "writing mats\n";
    Mat R =Mat_<double>::eye(3, 3),T = Mat_<double>::zeros(3, 1);
    cout << "R = " << R << "\n";
    cout << "T = " << T << "\n";
    fs << "R" << R;
    fs << "T" << T;

    cout << "writing MyData struct\n";
    MyData m(1);
    fs << "mdata" << m;
    cout << m << endl;
  }

  //read
  {
    FileStorage fs(filename, FileStorage::READ);

    if (!fs.isOpened())
    {
      cerr << "failed to open " << filename << endl;
      help(av);
      return 1;
    }

    FileNode n = fs["images"];
    if (n.type() != FileNode::SEQ)
    {
      cerr << "images is not a sequence! FAIL" << endl;
      return 1;
    }

    cout << "reading images\n";
    FileNodeIterator it = n.begin(), it_end = n.end();
    for (; it != it_end; ++it)
    {
      cout << (string)*it << "\n";
    }

    Mat R, T;
    cout << "reading R and T" << endl;

    fs["R"] >> R;
    fs["T"] >> T;

    cout << "R = " << R << "\n";
    cout << "T = " << T << endl;

    MyData m;
    fs["mdata"] >> m;

    cout << "read mdata\n";
    cout << m << endl;

Gary Bradski's avatar
Gary Bradski committed
153
    cout << "attempting to read mdata_b\n";   //Show default behavior for empty matrix
154 155 156 157 158 159
    fs["mdata_b"] >> m;
    cout << "read mdata_b\n";
    cout << m << endl;

  }

160 161 162 163 164
  cout << "Try opening " << filename << " to see the serialized data." << endl << endl;

  //read from string
  {
    cout << "Read data from string\n";
165
    string dataString =
166 167 168 169 170 171
        "%YAML:1.0\n"
        "mdata:\n"
        "   A: 97\n"
        "   X: 3.1415926535897931e+00\n"
        "   id: mydata1234\n";
    MyData m;
172
    FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    cout << "attempting to read mdata_b from string\n";   //Show default behavior for empty matrix
    fs["mdata"] >> m;
    cout << "read mdata\n";
    cout << m << endl;
  }

  //write to string
  {
    cout << "Write data to string\n";
    FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML);

    cout << "writing MyData struct\n";
    MyData m(1);
    fs << "mdata" << m;
    cout << m << endl;
    string createdString = fs.releaseAndGetString();
    cout << "Created string:\n" << createdString << "\n";
  }
191 192 193

  return 0;
}