Opencv learning notes (23) cv. Mat and. txt file data read and write operations

Source: Internet
Author: User

1. Read and Write. txt files in opencv format

You can use cvsave and cvload to implement the format and. XML /. yml is similar, but if it is dedicated to reading and writing data from opencv, it is still used. XML /. yml file format is better, I prefer it. yml format, good readability.

Use cvsave and cvload to read and write. txt files. The implementation method and data format are basically the same as that of. yml files.

For example, cvsave ("camera_matrix.txt", camera_matrix); // saves the array header of camera_matrix and the data it refers to (files in the yml format)

2. Import/Export the. txt file data of other programs

The common sprintf_s and fprintf_s can be used for implementation, but the efficiency is relatively low. Here we introduce a fast and easy-to-use method that uses steam and vector of STD.

 

# Include <iostream> # include <fstream> # include <iterator> # include <vector> using namespace STD;/* -------------------------- * function: Write CV: mat data. TXT file * ---------------------------- * function: writedata * Access: Public * return:-1: failed to open the file; 0: data written successfully; 1: blank matrix ** parameter: filename [in] file name * parameter: matdata [in] matrix data */INT writedata (string filename, CV: mat & matdata) {int retval = 0; // check whether the matrix is null if (matdata. empty () {cout <<"The matrix is empty" <Endl; retval = 1; Return (retval);} // open the file ofstream OUTFILE (filename. c_str (), ios_base: Out); // write if (! OUTFILE. is_open () {cout <"failed to open the file" <Endl; retval =-1; Return (retval) ;}// write data for (INT r = 0; r <matdata. rows; r ++) {for (int c = 0; C <matdata. cols; C ++) {int DATA = matdata. at <uchar> (R, c); // reads data. at <type>-type is the specific data format of matrix elements. OUTFILE <data <"\ t "; // data in each column is separated by a tab.} OUTFILE <Endl; // line feed} return (retval);}/* -------------------------- * function: From. read data in the TXT file and save it to the CV: mat matrix *-data is read in float format by default. *-if no If the number of rows, columns, and channels of the specified matrix is exists, the output matrix is * ------------------------ * function: loaddata * Access: Public * return:-1: failed to open the file; 0: data is successfully read by the specified matrix parameter; 1: Data is read by default matrix parameter ** parameter: filename [in] file name * parameter: matdata [out] matrix data * parameter: matrows [in] matrix number of rows. Default Value: 0 * parameter: matcols [in] matrix Number of columns. Default Value: 0 * parameter: matchns [in] Number of matrix channels. The default value is 0 */INT loaddata (string filename, CV: mat & matdata, int matrows = 0, int matcols = 0, int matchns = 0) {int retval = 0; // Open the ifstream infile (filename. c_str (), ios_base: In); If (! Infile. is_open () {cout <"failed to read the file" <Endl; retval =-1; Return (retval );} // load the data istream_iterator <float> begin (infile); // get the starting pointer of the file data stream in float format istream_iterator <float> end; // obtain the end position of the file stream. Vector <float> indata (begin, end); // save the file data to CV: mat tmpmat = CV: In STD: vector :: MAT (indata); // convert data from STD: vector to CV: mat // output to the command line window // copy (VEC. begin (), VEC. end (), ostream_iterator <double> (cout, "\ t"); // check the specified matrix size and number of channels size_t da Talength = indata. Size (); // 1. Number of channels if (matchns = 0) {matchns = 1 ;}// 2. Number of columns if (matrows! = 0 & matcols = 0) {matcols = datalength/matchns/matrows;} else if (matcols! = 0 & matrows = 0) {matrows = datalength/matchns/matcols;} else if (matcols = 0 & matrows = 0) {matrows = datalength/matchns; matcols = 1;} // 3. total Data Length if (datalength! = (Matrows * matcols * matchns) {cout <"the length of the data to be read does not meet the specified matrix size and number of channels. The matrix is output by default! "<Endl; retval = 1; matchns = 1; matrows = datalength;} // save the file data to the output matrix matdata = tmpmat. reshape (matchns, matrows ). clone (); Return (retval );}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.