读写FileStorage
using namespace std;
using namespace cv;
1.读FileStorage
void rFileStorage() {
FileStorage fs;
fs.open("d:/datum/test/test.json",
FileStorage::READ | FileStorage::FORMAT_JSON);
String sn = (String)fs["strvalue"];
double dn = (double)fs["doublevalue"];
Matx33d mat;
fs["matvalue"] >> mat;
cout << "matvalue: \n" << mat << endl;
cout << "doublevalue: " << dn << endl;
cout << "strvalue: " << sn << endl;
fs.release();
qDebug() << "------ read OK------";
}
2.写FileStorage
void wFileStorage() {
String str = "a random note";
double d = 999.001;
Matx33d mat = {1, 2, 3, 4, 5, 6, 7, 8, 9};
FileStorage fs;
fs.open("d:/datum/test/test.json", FileStorage::WRITE | FileStorage::FORMAT_JSON);
fs << "matvalue" << mat;
fs << "doublevalue" << d;
fs << "strvalue" << str;
fs.release();
qDebug() << "------write OK------";
}
3.JSON文件
{
"matvalue": {
"type_id": "opencv-matrix",
"rows": 3,
"cols": 3,
"dt": "d",
"data": [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]
},
"doublevalue": 9.9900099999999998e+02,
"strvalue": "a random note"
}