日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV FileStorage类的数据读写操作

發布時間:2024/4/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV FileStorage类的数据读写操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OpenCV FileStorage類的數據讀寫操作

OpenCV的許多應用都需要使用數據的存儲于讀取,例如經過3D校準后的相機,需要存儲校準結果矩陣,以方便下次調用該數據;基于機器學習的應用,同樣需要將學習得到的參數保存等。OpenCV通過XML/YAML格式實現數據持久化。本文簡要梳理了使用FileStorage類進行基本數據持久化操作,給出了示例代碼。

主要內容包括:

FileStorage

  • 構造函數
  • operator <<
  • FileStorage::open
  • FileStorage::isOpened
  • FileStorage::release
  • FileStorage::getFirstTopLevelNode
  • FileStorage::root
  • FileStorage::operator[]

示例代碼

  • 創建寫入器、創建讀取器
  • 寫入數值、寫入矩陣、寫入自定義數據結構、寫入當前時間
  • 讀取數值、讀取矩陣、讀取自定義數據結構、讀取當前時間
  • 關閉寫入器、關閉讀取器


FileStorage


FileStorage類將各種OpenCV數據結構的數據存儲為XML 或 YAML格式。同時,也可以將其他類型的數值數據存儲為這兩種格式。

構造函數

FileStorage類的構造函數為:

[cpp]?view plain?copy
  • cv::FileStorage(const?string&?source,?int?flags,?const?string&?encoding=string());??

  • 參數:

    source?–存儲或讀取數據的文件名(字符串),其擴展名(.xml?或?.yml/.yaml)決定文件格式。

    flags?–?操作模式,包括:

    • FileStorage::READ?打開文件進行讀操作
    • FileStorage::WRITE?打開文件進行寫操作
    • FileStorage::APPEND打開文件進行附加操作
    • FileStorage::MEMORY?從source讀數據,或向內部緩存寫入數據(由FileStorage::release返回)

    encoding?–?文件編碼方式。目前不支持UTF-16 XML 編碼,應使用 8-bit 編碼。

    寫數據operator <<

    向filestorage中寫入數據

    [cpp]?view plain?copy
  • template<typename_Tp>?FileStorage&?operator<<(FileStorage&?fs,?const?_Tp&?value)??
  • template<typename_Tp>?FileStorage&?operator<<(FileStorage&?fs,?const?vector<_Tp>&?vec)??
  • 參數:

    fs?– 已經打開的用于寫數據的file storage對象

    value?– 待寫入fs?的數據.

    vec?– 待寫入fs?的向量值

    ?

    以下代碼分別演示寫入數值、矩陣、多個變量、當前時間和關閉文件:

    [cpp]?view plain?copy
  • //?1.create?our?writter??
  • ????cv::FileStorage?fs("test.yml",?FileStorage::WRITE);??
  • ??????
  • ????//?2.Save?an?int??
  • ????int?imageWidth=?5;??
  • ????int?imageHeight=?10;??
  • ????fs?<<?"imageWidth"?<<?imageWidth;??
  • ????fs?<<?"imageHeight"?<<?imageHeight;??
  • ??
  • ????//?3.Write?a?Mat??
  • ????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;??
  • ??
  • ????//?4.Write?multi-variables???
  • ????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;??
  • ??
  • ????//?5.Save?local?time??
  • ????time_t?rawtime;?time(&rawtime);?//#include?<time.h>??
  • ????fs?<<?"calibrationDate"?<<?asctime(localtime(&rawtime));??
  • ??
  • ????//?6.close?the?file?opened??
  • ????fs.release();??
  • 生成的文件test.yml


    FileStorage::open

    打開一個文件

    [cpp]?view plain?copy
  • boolFileStorage::open(const?string&?filename,?int?flags,?const?string&encoding=string())??
  • 參數:

    filename?– 待打開的文件名,其擴展名(.xml?或?.yml/.yaml) 決定文件格式(XML 或 YAML)

    flags?– 操作模式。見構造函數

    encoding?– 文件編碼方式。

    [cpp]?view plain?copy
  • //?open?a?file??
  • ????cv::FileStorage?fs;??
  • ????fs.open("test.yml",FileStorage::WRITE);??
  • //?...?some?process?here??
  • ????fs.release();??

  • FileStorage::isOpened

    檢查文件是否已經打開,調用:

    [cpp]?view plain?copy
  • boolFileStorage::isOpened()??
  • 返回:

    ture?– 如果對象關聯了當前文件;

    false?– 其他情況。

    嘗試打開文件后調用此方法是個比較好的做法。

    [cpp]?view plain?copy
  • //?Checks?whether?the?file?is?opened??
  • ????cv::FileStorage?fs;??
  • ????fs.open("test.yml",FileStorage::WRITE);??
  • ??
  • ????bool?flag?=?fs.isOpened();??
  • ????cout<<"flag?=?"<<flag<<endl<<endl;??
  • //?if?failed?to?open?a?file??
  • ????if(!fs.isOpened()){??
  • ????????cout<<"failed?to?open?file?test.yml?"<<endl<<endl;??
  • ????????return?1;??
  • ????}??
  • 運行結果:


    FileStorage::release

    存儲或讀取操作完成后,需要關閉文件并釋放緩存,調用

    [cpp]?view plain?copy
  • void?FileStorage::release()??
  • [cpp]?view plain?copy
  • cv::FileStorage?fs("test.yml",?fs::WRITE);??
  • //...?some?processing?here??
  • fs.release();??

  • FileStorage::getFirstTopLevelNode

    返回映射(mapping)頂層的第一個元素:

    [cpp]?view plain?copy
  • FileStorage::getFirstTopLevelNode()??

  • [cpp]?view plain?copy
  • //?open?a?file?for?reading??
  • ????fs.open("test.yml",?FileStorage::READ);??
  • //?get?the?first?top?level?node??
  • ????int?firstNode?=?fs.getFirstTopLevelNode();??
  • ????cout<<"the?First?Top?Level?Node?=?"<<firstNode<<endl<<endl;??
  • 運行結果



    FileStorage::root

    返回頂層映射(mapping)

    [cpp]?view plain?copy
  • FileNode?FileStorage::root(int?streamidx=0)???

  • 參數:

    streamidx?– 從0開始的字符串索引。大部分情況文件中只有一個串,但YAML支持多個串,因此可以有多個。

    Returns:?頂層映射


    讀數據:FileStorage::operator[]

    返回指定的頂層映射元素

    [cpp]?view plain?copy
  • FileNode?FileStorage::operator[](const?string&?nodename)?const??
  • FileNode?FileStorage::operator[](const?char*?nodename)?const??
  • 參數:

    nodename?– 文件節點名(見下文FileNode類)

    返回:名稱為nodename的節點數據

    [cpp]?view plain?copy
  • //?read?data?using?operator?[]??
  • ????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;??
  • ??
  • //?read?Mat??
  • ????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;??
  • ??
  • //?read?string??
  • ????string?timeRead;??
  • ????fs["calibrationDate"]>>timeRead;??
  • ????cout<<"calibrationDate?readed?=?"<<timeRead<<endl<<endl;??
  • ??
  • ????fs.release();??
  • 運行結果

    轉載請注明出處(本文更新鏈接)http://blog.csdn.net/iracer/article/details/51339377

    總結

    以上是生活随笔為你收集整理的OpenCV FileStorage类的数据读写操作的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。