1. 程式人生 > 其它 >Data Persistence with cv::FileStorage(資料儲存)

Data Persistence with cv::FileStorage(資料儲存)

主要函式

構造一個檔案物件

FileStorage::FileStorage();

FileStorage::FileStorage( string fileName, int flag );

開啟檔案物件

FileStorage::open( string fileName, int flag );

讀入檔案資料

cv::Mat anArray;
myFileStorage["calibrationMatrix"] >> anArray;

int aNumber;
myFileStorage["someInteger"] >> aNumber;

int aNumber;
aNumber = (int)myFileStorage["someInteger"]; //mapping型別資料,KEY=someInteger

//或通過iterator
cout << (int)lbpVal[i] //sequence型別資料的的i個元素

關閉檔案

cv::FileStorage::release() 

寫出

將資料儲存在“*.xml” , “*.yml”裡

  • 構建檔案物件cv::FileStorage
  • 開啟檔案cv::FileStorage::open FileStorage::WRITE
  • 寫資料
    • mapping 鍵值對 標誌:{ }
    • sequence 一系列未命名條目 [ ]
  • 釋放檔案物件cv::FileStorage::release

讀入

  • 建立物件並開啟 FileStorage::READ

  • data can be read with
    |-([]操作符過載) cv::FileStorage::operator


    | |-型別        | []中的值
    | |-mapping | string KEY
    | |-sequence | integer
    | |
    | |-返回一個cv::FileNode型別的物件
    |-(檔案節點(file node)的迭代器)cv::FileNode::Iterator

  • 對於cv::FileNode型別的物件,如果為基本資料型別,可通過強轉(或隱式)直接獲取

  • 通過 = 或 >> 儲存在變數中

  • release

source code

//vs2019,OpenCV4.5.1
#include <iostream>
using namespace std;
#include <opencv2/core.hpp>
using namespace cv;
#include <ctime>

int main() {
	
	/************* 寫出 *************/
	FileStorage fs;
	fs.open("test.yml", FileStorage::WRITE);
	//FileStorage fs("test.yml", FileStorage::WRITE); // 構建+開啟

	fs << "frameCount" << 5;

	time_t rowTime;
	time(&rowTime);
	fs << "calibrationDate" << asctime(localtime(&rowTime));

	Mat caneraMatrix = (Mat_<double>(3,3)<<1000,0,320,0,1000,240,0,0,1);
	Mat distCoffs = (Mat_<double>(5, 1) << 0.1, 0.01, -0.001, 0, 0);

	fs << "caneraMatrix" << caneraMatrix << "distCoffs" << distCoffs;

	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();

	cout << "test.yml Done!" << endl;
	system("pause");

	/************* 讀入 *************/
	fs.open("test.yml", FileStorage::READ);

	//讀入法1, 等號
	int frameCount = (int)fs["frameCount"];

	//讀入法2, >>
	String calibrationDate;
	fs["calibrationDate"] >> calibrationDate;
	fs["caneraMatrix"] >> caneraMatrix;
	fs["distCoffs"] >> distCoffs;

	cout << "frameCount = " << frameCount
		<< "\ncalibrationDate = " << calibrationDate
		<< "\ncaneraMatrix = \n" << caneraMatrix
		<< "\n\ndistCoffs = \n" << distCoffs << endl;

	//通過迭代器訪問元素
	FileNode features = fs["features"];
	FileNodeIterator iterator = features.begin(), iteratorEnd = features.end();
	vector<unsigned char> lbpVal;

	for (; iterator != iteratorEnd; iterator++) {
		cout << "\nx = " << (int)(*iterator)["x"] << ", " << "y = " << (int)(*iterator)["y"]<<", "<<"lbp = [ ";
		(*iterator)["lbp"] >> lbpVal;
		for (int i{ 0 }; i < lbpVal.size(); i++) {
			cout << (int)lbpVal[i]<<" " ;
		}
		cout << " ]" << endl;
	}
	fs.release();
	return 0;
}

test.yml

%YAML:1.0
---
frameCount: 5
calibrationDate: "Mon Apr  4 16:11:42 2022\n"
caneraMatrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
distCoffs: !!opencv-matrix
   rows: 5
   cols: 1
   dt: d
   data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
       -1.0000000000000000e-03, 0., 0. ]
features:
   - { x:41, y:227, lbp:[ 0, 1, 1, 1, 1, 1, 0, 1 ] }
   - { x:260, y:449, lbp:[ 0, 0, 1, 1, 0, 1, 1, 0 ] }
   - { x:598, y:78, lbp:[ 0, 1, 0, 0, 1, 0, 1, 0 ] }

result