你将得到以下几个问题的答案:
代码
你可以点击此处下载或直接从OpenCV代码库中找到源文件。samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp。
以下用简单的示例代码演示如何逐一实现所有目的.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 |
#include <opencv2/core/core.hpp>
#include <iostream>
#include <string>
using namespace cv;
namespace std;
class MyData
{
public:
MyData(): A(0), X(id()
{}
explicit MyData(int) 97), X(CV_PI), id("mydata1234") // explicit 避免隐式类型转换
{}
void write(FileStorage& fs) const //Write serialization for this class
{
fs << "{" "A" << A "X" << X "id" << id "}";
}
void read(const FileNode& node) //Read serialization for this class
{
A = (int)node["A"];
X double)node["X"];
id = (string)node["id"];
}
: // Data Members
int A;
double X;
string id;
};
//These write and read functions must be defined for the serialization in FileStorage to work
& fs, const std::string&,32); font-weight:bold">const MyData& x)
{
x.write(fs);
}
& node, MyData& x,102)">& default_value = MyData()){
if(node.empty())
x = default_value;
else
x.read(node);
}
// This function will print our custom class to the console
ostream& operator<<(ostream& out,102)">& m)
{
out "{ id = " << m.id ",";
out "X = " << m.X "A = " << m.A "}";
return out;
}
int main(int ac, char** av)
{
if (ac != 2)
{
help(av);
return 1;
}
string filename = av[1];
{ //write
Mat R = Mat_<uchar>::eye(3, 3),
T <double>::zeros(1);
MyData m(1);
FileStorage fs(filename, FileStorage::WRITE);
fs "iterationNr" << 100;
fs "strings" "["; // text - string sequence
fs "image1.jpg" "Awesomeness" "baboon.jpg";
fs "]"; // close sequence
fs "Mapping"; // text - mapping
fs "One" 1;
fs << "Two" 2 "}";
fs "R" << R; // cv::Mat
fs "T" << T;
fs "MyData" << m; // your own data structures
fs.release(); // explicit close
cout "Write Done." << endl;
}
{//read
cout << endl "Reading: " << endl;
FileStorage fs;
fs.open(filename,102)">::READ);
int itNr;
//fs["iterationNr"] >> itNr;
itNr int) fs["iterationNr"];
cout << itNr;
if (!fs.isOpened())
{
cerr "Failed to open " << filename << endl;
help(av);
1;
}
FileNode n = fs["strings"]; // Read string sequence - Get node
if (n.type() != FileNode::SEQ)
{
cerr "strings is not a sequence! FAIL" << endl;
1;
}
FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
for (; it != it_end; ++it)
cout << (string)*it << endl;
n "Mapping"]; // Read mappings from a sequence
cout "Two " << (int)(n["Two"]) "; ";
cout "One " "One"]) << endl;
MyData m;
Mat R, T;
fs["R"] >> R; // Read cv::Mat
fs["T"] >> T;
fs["MyData"] >> m; // Read your own structure_
cout << endl
"R = " << R << endl;
cout "T = " << T "MyData = " << m << endl;
//Show default behavior for non existing nodes
cout "Attempt to read NonExisting (should initialize the data structure with its default).";
fs["NonExisting"] >> m;
cout "NonExisting = " << endl;
}
cout << endl
"Tip: Open up " " with a text editor to see the serialized data." << endl;
0;
}
|