OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
XML/YAML/JSON 持久化

詳細描述

XML/YAML/JSON 檔案儲存。

寫入檔案儲存。

您可以將各種 OpenCV 資料結構儲存到 XML (http://www.w3c.org/XML)、YAML (http://www.yaml.org) 或 JSON (http://www.json.org/) 格式,然後再從這些格式中恢復。此外,還可以儲存和載入任意複雜的資料結構,這些資料結構包括 OpenCV 資料結構,以及基本資料型別(整數和浮點數以及文字字串)作為其元素。

使用以下步驟將內容寫入 XML、YAML 或 JSON

  1. 建立新的 FileStorage 並開啟它以進行寫入。可以使用對 FileStorage::FileStorage 建構函式的單個呼叫來完成,該建構函式採用檔名,也可以使用預設建構函式,然後呼叫 FileStorage::open。檔案格式(XML、YAML 或 JSON)由檔名副檔名決定(分別為“.xml”、“.yml”/“.yaml”和“.json”)
  2. 使用流運算子 << 寫入所有要寫入的資料,就像 STL 流的情況一樣。
  3. 使用 FileStorage::release 關閉檔案。FileStorage 解構函式也會關閉檔案。

這是一個例子

#include "opencv2/core.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
fs << "frameCount" << 5;
time_t rawtime; time(&rawtime);
fs << "calibrationDate" << asctime(localtime(&rawtime));
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
fs << "features" << "[";
for( int i = 0; i < 3; i++ )
{
int x = rand() % 640;
int y = rand() % 480;
uchar lbp = rand() % 256;
fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
for( int j = 0; j < 8; j++ )
fs << ((lbp >> j) & 1);
fs << "]" << "}";
}
fs << "]";
fs.release();
return 0;
}
XML/YAML/JSON 檔案儲存類,封裝了寫入或讀取資料所需的所有資訊...
定義 persistence.hpp:261
@ WRITE
value,開啟檔案以進行寫入
定義 persistence.hpp:267
從 Mat 派生的模板矩陣類。
定義 mat.hpp:2257
n 維密集陣列類
定義 mat.hpp:830
void release()
遞減引用計數器並在需要時釋放矩陣。
I.at<uchar>(y, x) = saturate_cast<uchar>(r);
uchar
unsigned char uchar
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3
定義 core.hpp:107

上面的示例將整數、文字字串(校準日期)、2 個矩陣和一個自定義結構“feature”儲存到 YML,其中包含特徵座標和 LBP(區域性二值模式)值。以下是示例的輸出

%YAML:1.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011\n"
cameraMatrix: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
distCoeffs: !!opencv-matrix
rows: 5
cols: 1
dt: d
data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
-1.0000000000000000e-03, 0., 0. ]
features
- { x:167, y:49, lbp:[ 1, 0, 0, 1, 1, 0, 1, 1 ] }
- { x:298, y:130, lbp:[ 0, 0, 0, 1, 0, 0, 1, 1 ] }
- { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] }

作為練習,您可以將示例中的“.yml”替換為“.xml”或“.json”,並檢視相應的 XML 檔案會是什麼樣子。

透過檢視示例程式碼和輸出,可以注意到以下幾點

從檔案儲存中讀取資料。

要讀取先前寫入的 XML、YAML 或 JSON 檔案,請執行以下操作

  1. 使用 FileStorage::FileStorage 建構函式或 FileStorage::open 方法開啟檔案儲存。在當前的實現中,整個檔案被解析,並且檔案儲存的整個表示在記憶體中構建為檔案節點的層次結構(參見 FileNode
  2. 讀取您感興趣的資料。使用 FileStorage::operator []FileNode::operator [] 和/或 FileNodeIterator
  3. 使用 FileStorage::release 關閉儲存。

以下是如何讀取由上面的程式碼示例建立的檔案

FileStorage fs2("test.yml", FileStorage::READ);
// 第一種方法:在 FileNode 上使用 (type) 運算子。
int frameCount = (int)fs2["frameCount"];
String date;
// 第二種方法:使用 FileNode::operator >>
fs2["calibrationDate"] >> date;
Mat cameraMatrix2, distCoeffs2;
fs2["cameraMatrix"] >> cameraMatrix2;
fs2["distCoeffs"] >> distCoeffs2;
cout << "frameCount: " << frameCount << endl
<< "calibration date: " << date << endl
<< "camera matrix: " << cameraMatrix2 << endl
<< "distortion coeffs: " << distCoeffs2 << endl;
FileNode features = fs2["features"];
FileNodeIterator it = features.begin(), it_end = features.end();
int idx = 0;
std::vector<uchar> lbpval;
// 使用 FileNodeIterator 迭代序列
for( ; it != it_end; ++it, idx++ )
{
cout << "feature #" << idx << ": ";
cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
// 您還可以使用 FileNode >> std::vector 運算子輕鬆讀取數值陣列。
(*it)["lbp"] >> lbpval;
for( int i = 0; i < (int)lbpval.size(); i++ )
cout << " " << (int)lbpval[i];
cout << ")" << endl;
}
fs2.release();
用於迭代序列和對映。
定義 persistence.hpp:595
檔案儲存節點類。
定義 persistence.hpp:441
FileNodeIterator begin() const
返回指向第一個節點元素的迭代器
FileNodeIterator end() const
返回指向最後一個節點元素之後的元素的迭代器
@ READ
值,開啟檔案進行讀取
定義 persistence.hpp:266
std::string String
定義 cvstd.hpp:151

格式規範

([count]{u|c|w|s|i|f|d})... 其中字元對應於基本 C++ 型別

count 是給定型別的可選值計數器。例如,2if 表示每個陣列元素是一個包含 2 個整數的結構,後跟一個單精度浮點數。上述規範的等效表示法是 iif2i1f 等。其他示例:u 表示陣列由位元組組成,2d 表示陣列由雙精度對組成。

另請參見
samples/cpp/tutorial_code/core/file_input_output/file_input_output.cpp

類  cv::FileNode
 檔案儲存 Node 類。更多...
 
類  cv::FileNodeIterator
 用於迭代序列和對映。更多...
 
類  cv::FileStorage
 XML/YAML/JSON 檔案儲存類,封裝了將資料寫入檔案或從檔案讀取資料所需的所有資訊。更多...