1. 程式人生 > >OpenCV中將Mat,vector型別存為txt格式

OpenCV中將Mat,vector型別存為txt格式

C++: FileStorage::FileStorage(const string& source, int flags, const string& encoding=string())

Parameters:
  • source – Name of the file to open or the text string to read the data from. Extension of the file (.xml or .yml/.yaml
    ) determines its format (XML or YAML respectively). Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify the output file format (e.g. mydata.xml
    .yml etc.).你要建立的檔案的名字和型別
  • flags –

    Mode of operation. Possible values are:

    • FileStorage::READ Open the file for reading.
    • FileStorage::WRITE Open the file for writing.
    • FileStorage::APPEND Open the file for appending.
    • FileStorage::MEMORY
       Read data from source or write data to the internal buffer (which is returned by FileStorage::release)
  • encoding – Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it.

 

FileStorage cp("calipara.txt",FileStorage::WRITE);
cp<<"intrinsic"<<intrinsic;
cp<<"DistCoefs"<<distCoeffs;
cp<<"rotation_matrix"<<rotation_matrix;
cp<<"trans_vec"<<cameraCalibrator.tvecs;
cp.release();

直接就是上面的程式碼,用FileStorage,就行了。想換成.xml格式則把字尾.txt改成.xml就可以了。

放上效果圖