Writing to FileStorage
To write a file with some OpenCV or other numeric data, we can use the FileStorage class, using a streaming << operator such as STL streaming:
#include "opencv2/opencv.hpp" using namespace cv; int main(int, char** argv) { // create our writer FileStorage fs("test.yml", FileStorage::WRITE); // Save an int int fps= 5; fs << "fps" << fps; // Create some mat sample Mat m1= Mat::eye(2,3, CV_32F); Mat m2= Mat::ones(3,2, CV_32F); Mat result= (m1+1).mul(m1+3); // write the result fs << "Result" << result; // release the file fs.release(); FileStorage fs2("test.yml", FileStorage::READ); Mat r; fs2["Result"] >> r; std::cout << r << std::endl; fs2.release(); return 0; }
To create a file storage where we save the data, we only need to call the constructor, giving a path filename with the extension format desired (XML or YAML), and the second parameter set to write:
FileStorage fs("test.yml", FileStorage::WRITE);
If we want to save data, we only need to use the stream operator by giving an identifier in the first stage, and later the matrix or value that we want to save. For example, to save an int variable, we only have to write the following lines of code:
int fps= 5; fs << "fps" << fps;
Otherwise, we can write/save mat as shown:
Mat m1= Mat::eye(2,3, CV_32F); Mat m2= Mat::ones(3,2, CV_32F); Mat result= (m1+1).mul(m1+3); // write the result fs << "Result" << result;
The result of the preceding code is a YAML format:
%YAML:1.0 fps: 5 Result: !!opencv-matrix rows: 2 cols: 3 dt: f data: [ 8., 3., 3., 3., 8., 3. ]
Reading from a file storage to read a file saved previously is very similar to the save functions:
#include "opencv2/opencv.hpp" using namespace cv; int main(int, char** argv) { FileStorage fs2("test.yml", FileStorage::READ); Mat r; fs2["Result"] >> r; std::cout << r << std::endl; fs2.release(); return 0; }
The first stage is to open a saved file with the FileStorage constructor using the appropriate parameters, path, and FileStorage::READ:
FileStorage fs2("test.yml", FileStorage::READ);
To read any stored variable, we only need to use the common stream operator >> using our FileStorage object and the identifier with the [] operator:
Mat r; fs2["Result"] >> r;
- Hands-On Data Structures and Algorithms with Rust
- iOS and OS X Network Programming Cookbook
- 達夢數據庫性能優化
- Doris實時數倉實戰
- Unity 2018 By Example(Second Edition)
- 數據庫應用系統技術
- 中國云存儲發展報告
- 企業級大數據項目實戰:用戶搜索行為分析系統從0到1
- 數據應用工程:方法論與實踐
- 標簽類目體系:面向業務的數據資產設計方法論
- ECharts數據可視化:入門、實戰與進階
- SQL Server 2012 數據庫教程(第3版)
- Learn Selenium
- Creating Mobile Apps with Appcelerator Titanium
- 大數據用戶行為畫像分析實操指南