OpenCV —FileStorage類的數(shù)據(jù)讀寫操作與示例
OpenCV的許多應用都需要使用數(shù)據(jù)的存儲于讀取,例如經(jīng)過3D校準后的相機,需要存儲校準結果矩陣,以方便下次調用該數(shù)據(jù);基于機器學習的應用,同樣需要將學習得到的參數(shù)保存等。OpenCV通過XML/YAML格式實現(xiàn)數(shù)據(jù)持久化。本文簡要梳理了使用FileStorage類進行基本數(shù)據(jù)持久化操作,給出了示例代碼。
主要內容包括:
FileStorage類
- 構造函數(shù)
- operator <<
- FileStorage::open
- FileStorage::isOpened
- FileStorage::release
- FileStorage::getFirstTopLevelNode
- FileStorage::root
- FileStorage::operator[]
示例代碼
- 創(chuàng)建寫入器、創(chuàng)建讀取器
- 寫入數(shù)值、寫入矩陣、寫入自定義數(shù)據(jù)結構、寫入當前時間
- 讀取數(shù)值、讀取矩陣、讀取自定義數(shù)據(jù)結構、讀取當前時間
- 關閉寫入器、關閉讀取器
FileStorage類
FileStorage類將各種OpenCV數(shù)據(jù)結構的數(shù)據(jù)存儲為XML 或 YAML格式。同時,也可以將其他類型的數(shù)值數(shù)據(jù)存儲為這兩種格式。
構造函數(shù)
FileStorage類的構造函數(shù)為:
[cpp]?view plaincopy
cv::FileStorage(const?string&?source,?int?flags,?const?string&?encoding=string());??
參數(shù):
source?–存儲或讀取數(shù)據(jù)的文件名(字符串),其擴展名(.xml?或?.yml/.yaml)決定文件格式。
flags?–?操作模式,包括:
- FileStorage::READ?打開文件進行讀操作
- FileStorage::WRITE?打開文件進行寫操作
- FileStorage::APPEND打開文件進行附加操作
- FileStorage::MEMORY?從source讀數(shù)據(jù),或向內部緩存寫入數(shù)據(jù)(由FileStorage::release返回)
encoding?–?文件編碼方式。目前不支持UTF-16 XML 編碼,應使用 8-bit 編碼。
寫數(shù)據(jù)operator <<
向filestorage中寫入數(shù)據(jù)
[cpp]?view plaincopy
template<typename_Tp>?FileStorage&?operator<<(FileStorage&?fs,?const?_Tp&?value)??template<typename_Tp>?FileStorage&?operator<<(FileStorage&?fs,?const?vector<_Tp>&?vec)??
參數(shù):
fs?– 已經(jīng)打開的用于寫數(shù)據(jù)的file storage對象
value?– 待寫入fs?的數(shù)據(jù).
vec?– 待寫入fs?的向量值
?
以下代碼分別演示寫入數(shù)值、矩陣、多個變量、當前時間和關閉文件:
[cpp]?view plaincopy
??????cv::FileStorage?fs("test.yml",?FileStorage::WRITE);??????????????????int?imageWidth=?5;??????int?imageHeight=?10;??????fs?<<?"imageWidth"?<<?imageWidth;??????fs?<<?"imageHeight"?<<?imageHeight;??????????????cv::Mat?m1=?Mat::eye(3,3,?CV_8U);??????cv::Mat?m2=?Mat::ones(3,3,?CV_8U);??????cv::Mat?resultMat=?(m1+1).mul(m1+2);??????fs?<<?"resultMat"?<<?resultMat;??????????????cv::Mat?cameraMatrix?=?(Mat_<double>(3,3)?<<?1000,?0,?320,?0,?1000,?240,?0,?0,?1);??????cv::Mat?distCoeffs?=?(Mat_<double>(5,1)?<<?0.1,?0.01,?-0.001,?0,?0);??????fs?<<?"cameraMatrix"?<<?cameraMatrix?<<?"distCoeffs"?<<?distCoeffs;??????????????time_t?rawtime;?time(&rawtime);???????fs?<<?"calibrationDate"?<<?asctime(localtime(&rawtime));??????????????fs.release();??
生成的文件test.yml
FileStorage::open
打開一個文件
[cpp]?view plaincopy
boolFileStorage::open(const?string&?filename,?int?flags,?const?string&encoding=string())??
參數(shù):
filename?– 待打開的文件名,其擴展名(.xml?或?.yml/.yaml) 決定文件格式(XML 或 YAML)
flags?– 操作模式。見構造函數(shù)
encoding?– 文件編碼方式。
[cpp]?view plaincopy
??????cv::FileStorage?fs;??????fs.open("test.yml",FileStorage::WRITE);????????fs.release();??
FileStorage::isOpened
檢查文件是否已經(jīng)打開,調用:
[cpp]?view plaincopy
boolFileStorage::isOpened()??
返回:
ture?– 如果對象關聯(lián)了當前文件;
false?– 其他情況。
嘗試打開文件后調用此方法是個比較好的做法。
[cpp]?view plaincopy
??????cv::FileStorage?fs;??????fs.open("test.yml",FileStorage::WRITE);????????bool?flag?=?fs.isOpened();??????cout<<"flag?=?"<<flag<<endl<<endl;????????if(!fs.isOpened()){??????????cout<<"failed?to?open?file?test.yml?"<<endl<<endl;??????????return?1;??????}??
運行結果:
FileStorage::release
存儲或讀取操作完成后,需要關閉文件并釋放緩存,調用
[cpp]?view plaincopy
void?FileStorage::release()??
[cpp]?view plaincopy
cv::FileStorage?fs("test.yml",?fs::WRITE);????fs.release();??
FileStorage::getFirstTopLevelNode
返回映射(mapping)頂層的第一個元素:
[cpp]?view plaincopy
FileStorage::getFirstTopLevelNode()??
[cpp]?view plaincopy
??????fs.open("test.yml",?FileStorage::READ);????????int?firstNode?=?fs.getFirstTopLevelNode();??????cout<<"the?First?Top?Level?Node?=?"<<firstNode<<endl<<endl;??
運行結果
FileStorage::root
返回頂層映射(mapping)
[cpp]?view plaincopy
FileNode?FileStorage::root(int?streamidx=0)???
參數(shù):
streamidx?– 從0開始的字符串索引。大部分情況文件中只有一個串,但YAML支持多個串,因此可以有多個。
Returns:?頂層映射
讀數(shù)據(jù):FileStorage::operator[]
返回指定的頂層映射元素
[cpp]?view plaincopy
FileNode?FileStorage::operator[](const?string&?nodename)?const??FileNode?FileStorage::operator[](const?char*?nodename)?const??
參數(shù):
nodename?– 文件節(jié)點名(見下文FileNode類)
返回:名稱為nodename的節(jié)點數(shù)據(jù)
[cpp]?view plaincopy
??????cv::FileStorage?fs("test.yml",?FileStorage::READ);??????int?width;??????int?height;??????fs["imageWidth"]>>width;??????fs["imageHeight"]>>height;??????cout<<"width?readed?=?"<<width<<endl;??????cout<<"height?readed?=?"<<height<<endl<<endl;??????????cv::Mat?resultMatRead;??????fs["resultMat"]>>resultMatRead;??????cout<<"resultMat?readed?=?"<<resultMatRead<<endl<<endl;????????????cv::Mat?cameraMatrixRead,distCoeffsRead;??????fs["cameraMatrix"]>>cameraMatrixRead;??????fs["distCoeffs"]>>distCoeffsRead;??????cout<<"cameraMatrix?readed?=?"<<cameraMatrixRead<<endl;??????cout<<"distCoeffs?readed?=?"<<distCoeffsRead<<endl<<endl;??????????string?timeRead;??????fs["calibrationDate"]>>timeRead;??????cout<<"calibrationDate?readed?=?"<<timeRead<<endl<<endl;????????fs.release();??
運行結果
轉載請注明出處(本文更新鏈接):http://blog.csdn.net/iracer/article/details/51339377
OpenCV —FileStorage類的數(shù)據(jù)讀寫操作與示例
OpenCV的許多應用都需要使用數(shù)據(jù)的存儲于讀取,例如經(jīng)過3D校準后的相機,需要存儲校準結果矩陣,以方便下次調用該數(shù)據(jù);基于機器學習的應用,同樣需要將學習得到的參數(shù)保存等。OpenCV通過XML/YAML格式實現(xiàn)數(shù)據(jù)持久化。本文簡要梳理了使用FileStorage類進行基本數(shù)據(jù)持久化操作,給出了示例代碼。
主要內容包括:
FileStorage類
- 構造函數(shù)
- operator <<
- FileStorage::open
- FileStorage::isOpened
- FileStorage::release
- FileStorage::getFirstTopLevelNode
- FileStorage::root
- FileStorage::operator[]
示例代碼
- 創(chuàng)建寫入器、創(chuàng)建讀取器
- 寫入數(shù)值、寫入矩陣、寫入自定義數(shù)據(jù)結構、寫入當前時間
- 讀取數(shù)值、讀取矩陣、讀取自定義數(shù)據(jù)結構、讀取當前時間
- 關閉寫入器、關閉讀取器
FileStorage類
FileStorage類將各種OpenCV數(shù)據(jù)結構的數(shù)據(jù)存儲為XML 或 YAML格式。同時,也可以將其他類型的數(shù)值數(shù)據(jù)存儲為這兩種格式。
構造函數(shù)
FileStorage類的構造函數(shù)為:
[cpp]?view plaincopy
cv::FileStorage(const?string&?source,?int?flags,?const?string&?encoding=string());??參數(shù):source?–存儲或讀取數(shù)據(jù)的文件名(字符串),其擴展名(.xml?或?.yml/.yaml)決定文件格式。
flags?–?操作模式,包括:
- FileStorage::READ?打開文件進行讀操作
- FileStorage::WRITE?打開文件進行寫操作
- FileStorage::APPEND打開文件進行附加操作
- FileStorage::MEMORY?從source讀數(shù)據(jù),或向內部緩存寫入數(shù)據(jù)(由FileStorage::release返回)
encoding?–?文件編碼方式。目前不支持UTF-16 XML 編碼,應使用 8-bit 編碼。
寫數(shù)據(jù)operator <<
向filestorage中寫入數(shù)據(jù)
[cpp]?view plaincopy
template<typename_Tp>?FileStorage&?operator<<(FileStorage&?fs,?const?_Tp&?value)??template<typename_Tp>?FileStorage&?operator<<(FileStorage&?fs,?const?vector<_Tp>&?vec)??參數(shù):fs?– 已經(jīng)打開的用于寫數(shù)據(jù)的file storage對象
value?– 待寫入fs?的數(shù)據(jù).
vec?– 待寫入fs?的向量值
?
以下代碼分別演示寫入數(shù)值、矩陣、多個變量、當前時間和關閉文件:[cpp]?view plaincopy
??????cv::FileStorage?fs("test.yml",?FileStorage::WRITE);??????????????????int?imageWidth=?5;??????int?imageHeight=?10;??????fs?<<?"imageWidth"?<<?imageWidth;??????fs?<<?"imageHeight"?<<?imageHeight;??????????????cv::Mat?m1=?Mat::eye(3,3,?CV_8U);??????cv::Mat?m2=?Mat::ones(3,3,?CV_8U);??????cv::Mat?resultMat=?(m1+1).mul(m1+2);??????fs?<<?"resultMat"?<<?resultMat;??????????????cv::Mat?cameraMatrix?=?(Mat_<double>(3,3)?<<?1000,?0,?320,?0,?1000,?240,?0,?0,?1);??????cv::Mat?distCoeffs?=?(Mat_<double>(5,1)?<<?0.1,?0.01,?-0.001,?0,?0);??????fs?<<?"cameraMatrix"?<<?cameraMatrix?<<?"distCoeffs"?<<?distCoeffs;??????????????time_t?rawtime;?time(&rawtime);???????fs?<<?"calibrationDate"?<<?asctime(localtime(&rawtime));??????????????fs.release();??生成的文件test.yml
FileStorage::open
打開一個文件
[cpp]?view plaincopy
boolFileStorage::open(const?string&?filename,?int?flags,?const?string&encoding=string())??參數(shù):
filename?– 待打開的文件名,其擴展名(.xml?或?.yml/.yaml) 決定文件格式(XML 或 YAML)
flags?– 操作模式。見構造函數(shù)
encoding?– 文件編碼方式。
[cpp]?view plaincopy
??????cv::FileStorage?fs;??????fs.open("test.yml",FileStorage::WRITE);????????fs.release();??
FileStorage::isOpened
檢查文件是否已經(jīng)打開,調用:
[cpp]?view plaincopy
boolFileStorage::isOpened()??返回:
ture?– 如果對象關聯(lián)了當前文件;
false?– 其他情況。
嘗試打開文件后調用此方法是個比較好的做法。
[cpp]?view plaincopy
??????cv::FileStorage?fs;??????fs.open("test.yml",FileStorage::WRITE);????????bool?flag?=?fs.isOpened();??????cout<<"flag?=?"<<flag<<endl<<endl;????????if(!fs.isOpened()){??????????cout<<"failed?to?open?file?test.yml?"<<endl<<endl;??????????return?1;??????}??運行結果:
FileStorage::release
存儲或讀取操作完成后,需要關閉文件并釋放緩存,調用
[cpp]?view plaincopy
void?FileStorage::release()??[cpp]?view plaincopy
cv::FileStorage?fs("test.yml",?fs::WRITE);????fs.release();??FileStorage::getFirstTopLevelNode
返回映射(mapping)頂層的第一個元素:
[cpp]?view plaincopy
FileStorage::getFirstTopLevelNode()??[cpp]?view plaincopy
??????fs.open("test.yml",?FileStorage::READ);????????int?firstNode?=?fs.getFirstTopLevelNode();??????cout<<"the?First?Top?Level?Node?=?"<<firstNode<<endl<<endl;??運行結果
FileStorage::root
返回頂層映射(mapping)
[cpp]?view plaincopy
FileNode?FileStorage::root(int?streamidx=0)???參數(shù):
streamidx?– 從0開始的字符串索引。大部分情況文件中只有一個串,但YAML支持多個串,因此可以有多個。
Returns:?頂層映射
讀數(shù)據(jù):FileStorage::operator[]
返回指定的頂層映射元素
[cpp]?view plaincopy
FileNode?FileStorage::operator[](const?string&?nodename)?const??FileNode?FileStorage::operator[](const?char*?nodename)?const??參數(shù):
nodename?– 文件節(jié)點名(見下文FileNode類)
返回:名稱為nodename的節(jié)點數(shù)據(jù)
[cpp]?view plaincopy
??????cv::FileStorage?fs("test.yml",?FileStorage::READ);??????int?width;??????int?height;??????fs["imageWidth"]>>width;??????fs["imageHeight"]>>height;??????cout<<"width?readed?=?"<<width<<endl;??????cout<<"height?readed?=?"<<height<<endl<<endl;??????????cv::Mat?resultMatRead;??????fs["resultMat"]>>resultMatRead;??????cout<<"resultMat?readed?=?"<<resultMatRead<<endl<<endl;????????????cv::Mat?cameraMatrixRead,distCoeffsRead;??????fs["cameraMatrix"]>>cameraMatrixRead;??????fs["distCoeffs"]>>distCoeffsRead;??????cout<<"cameraMatrix?readed?=?"<<cameraMatrixRead<<endl;??????cout<<"distCoeffs?readed?=?"<<distCoeffsRead<<endl<<endl;??????????string?timeRead;??????fs["calibrationDate"]>>timeRead;??????cout<<"calibrationDate?readed?=?"<<timeRead<<endl<<endl;????????fs.release();??運行結果
轉載請注明出處(本文更新鏈接):http://blog.csdn.net/iracer/article/details/51339377
總結
以上是生活随笔為你收集整理的OpenCV —数据持久化: FileStorage类的数据存取操作与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。