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

  • 2020-06-03 07:56:44
  • OfStack

This article introduces OpenCV cv. Mat and.txt file data reading and writing operations, this site feels quite good, now share with you.

1. Read and write.txt files in OpenCV format

cvSave and cvLoad can be used, the format is similar to.xml /.yml, but if you use OpenCV for data reading and writing, it is better to use.xml /.yml file format, I prefer.yml format, it is very readable.

cvSave and cvLoad are used to read and write.txt files, and the implementation and data format are basically 1.

For example: cvSave (" camera_matrix. txt, "camera_matrix); // Saves the array header of camera_matrix and the file it refers to data (similar to yml)

2. Import/export.txt file data of other programs

Can use the regular sprintf_s and fprintf_s to achieve, but the efficiency is relatively low, here introduces a quick and easy method, using std's steam and vector.


#include <iostream> 
#include <fstream> 
#include <iterator> 
#include <vector> 
 
using namespace std; 
 
/*---------------------------- 
 *  function  :  will  cv::Mat  Write data to  .txt  file  
 *---------------------------- 
 *  function  : WriteData 
 *  access  : public 
 *  return  : -1 : Failed to open file; 0 : Write data successfully; 1 : The matrix is empty  
 * 
 *  parameter  : fileName [in]  The file name  
 *  parameter  : matData [in]  Matrix data  
 */ 
int WriteData(string fileName, cv::Mat& matData) 
{ 
 int retVal = 0; 
 
 //  Open the file  
 ofstream outFile(fileName.c_str(), ios_base::out); // Write as new or overridden  
 if (!outFile.is_open()) 
 { 
 cout << " Failed to open file " << endl; 
 retVal = -1; 
 return (retVal); 
 } 
 
 //  Check that the matrix is empty  
 if (matData.empty()) 
 { 
 cout << " The matrix of the empty " << endl; 
 retVal = 1; 
 return (retVal); 
 } 
 
 //  Write data  
 for (int r = 0; r < matData.rows; r++) 
 { 
 for (int c = 0; c < matData.cols; c++) 
 { 
 uchar data = matData.at<uchar>(r,c); // Read the data, at<type> - type  Is the specific data format of a matrix element  
 outFile << data << "\t" ; // For each column of data  tab  separated  
 } 
 outFile << endl; // A newline  
 } 
 
 return (retVal); 
} 
 
 /*---------------------------- 
 *  function  :  from  .txt  Read in the file and save to  cv::Mat  matrix  
 * -  The default in accordance with the  float  Format to read in data,  
 * -  If the number of rows, columns, and channels of the matrix is not specified, the output matrix is a single-channel, N  line  1  The column  
 *---------------------------- 
 *  function  : LoadData 
 *  access  : public 
 *  return  : -1 : Failed to open file; 0 : Read the data successfully according to the set matrix parameters; 1 : Read the data as the default matrix parameter  
 * 
 *  parameter  : fileName [in]  The file name  
 *  parameter  : matData [out]  Matrix data  
 *  parameter  : matRows [in]  The number of rows in the matrix, default is  0 
 *  parameter  : matCols [in]  The number of columns in the matrix, default is  0 
 *  parameter  : matChns [in]  Matrix number of channels, default is  0 
 */ 
int LoadData(string fileName, cv::Mat& matData, int matRows = 0, int matCols = 0, int matChns = 0) 
{ 
 int retVal = 0; 
 
 //  Open the file  
 ifstream inFile(fileName.c_str(), ios_base::in); 
 if(!inFile.is_open()) 
 { 
 cout << " Failed to read file " << endl; 
 retVal = -1; 
 return (retVal); 
 } 
 
 //  Load the data  
 istream_iterator<float> begin(inFile); // According to the  float  Format retrieves the start pointer to the file data stream  
 istream_iterator<float> end; // Gets the end of the file stream  
 vector<float> inData(begin,end); // Save file data to  std::vector  In the  
 cv::Mat tmpMat = cv::Mat(inData); // The data by  std::vector  convert  cv::Mat 
 
 //  Output to a command line window  
 //copy(vec.begin(),vec.end(),ostream_iterator<double>(cout,"\t")); 
 
 //  Check the set matrix size and the number of channels  
 size_t dataLength = inData.size(); 
 //1. The channel number  
 if (matChns == 0) 
 { 
 matChns = 1; 
 } 
 //2. The ranks of the number  
 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 in   Does not meet the   Set the matrix size and channel number requirements, the default mode will output the 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 articles: