OpenCV CV. Mat and. txt file data read and write operations

Source: Internet
Author: User
This article mainly introduces the OPENCV CV. Mat and. txt file data read and write operation, now share to everyone, but also for everyone to make a reference

1.. txt file read/write according to OPENCV format
Can be implemented with Cvsave and cvload, the format and. Xml/.yml, but if the private and OpenCV data read and write, or use. xml/.yml file format is good, I prefer. yml format, readability is great.
Read and write. txt files with Cvsave and Cvload, with the same implementation and data format as the. yml file.
For example: Cvsave ("Camera_matrix.txt", Camera_matrix); The array header of the Camera_matrix is saved and the data it refers to (files that resemble the Yml form)

2.. txt file data for importing/exporting other programs
Can be implemented with regular sprintf_s and fprintf_s, but with low efficiency, here's a quick and easy way to use the Std steam and vector.

#include <iostream> #include <fstream> #include <iterator> #include <vector> using namespace std  ; /*----------------------------* Function: Write Cv::mat data to a. txt file *----------------------------* Function: WriteData * Access: PU Blic * Return:-1: Open file failed; 0: Write data succeeded; 1: Matrix empty * parameter: filename [in] filename * parameter: matdata [in] matrix data */int WriteData (string    FileName, cv::mat& matdata) {int retVal = 0; Open File Ofstream outFile (Filename.c_str (), ios_base::out);      Write as new or overwrite to if (!outfile.is_open ()) {cout << "Open file failed" << Endl;     RetVal =-1;   return (RetVal);      }//Check whether the matrix is empty if (Matdata.empty ()) {cout << "matrix is empty" << Endl;     RetVal = 1;   return (RetVal); }//write data for (int r = 0, R < matdata.rows; r++) {for (int c = 0; c < Matdata.cols; C + +) {Uch AR data = matdata.at<uchar> (R,C);  Read Data,at<type>-type is the specific data format of the matrix element outFile << data << "\ T";   tab-delimited data for each column  } outFile << Endl; Line break} return (RetVal); }/*----------------------------* Function: reads data from a. txt file, saves it to the Cv::mat matrix *-Reads the data in float format by default, *-If you do not specify the number of rows, columns, and channels of the Matrix, The output matrix is a single-channel, N-row, 1-column *----------------------------* function: LoadData * Access: Public * return:-1: Open file failed; 0: Read data according to set matrix parameters successfully; 1: By default Read Data * parameter: filename [in] filename * parameter: matdata [out] Matrix data * parameter: matrows [in] matrix row number, default is 0 * parameter: matcols [in ] Number of matrix columns, default is 0 * parameter: Matchns [in] matrix channel number, default is 0 */int loaddata (string fileName, cv::mat& matdata, int matrows = 0, I    NT Matcols = 0, int matchns = 0) {int retVal = 0;   Open File Ifstream inFile (Filename.c_str (), ios_base::in);     if (!infile.is_open ()) {cout << "read file failed" << Endl;     RetVal =-1;   return (RetVal);  }//Load data istream_iterator<float> begin (InFile);     The starting pointer istream_iterator<float> end of the file data stream in float format;   The end of the file stream is taken vector<float> inData (begin,end); Save file data to std::vector cv::mat Tmpmat = CV:: Mat (InData);     Convert data from std::vector to Cv::mat//output to command Line window//copy (Vec.begin (), Vec.end (),ostream_iterator<double> (cout, "\ T"));   Check the set matrix size and number of channels size_t datalength = Indata.size ();   1. Number of channels if (Matchns = = 0) {Matchns = 1;   }//2. Number of rows 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 read does not meet the required matrix size and number of channels, will be output by default Matrix!     "<< Endl;     RetVal = 1;     Matchns = 1;   Matrows = datalength;      }//Save the file data to the output matrix Matdata = Tmpmat.reshape (Matchns, Matrows). Clone (); return (RetVal); }

Related Article

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.