【问题标题】:How to read the text file and create Mat object in C++如何在 C++ 中读取文本文件并创建 Mat 对象
【发布时间】:2013-10-12 14:38:32
【问题描述】:

您好,我已经能够将 Mat 对象写入文本文件。如下,

std::fstream outputFile;
    outputFile.open( "myFile.txt", std::ios::out ) ;

    outputFile << des_object.rows << std::endl;
    outputFile << des_object.cols << std::endl;

    for(int i=0; i<des_object.rows; i++)
    {
        for(int j=0; j<des_object.cols; j++)
        {
            outputFile << des_object.at<float>(i,j) << std::endl;
        }

    }
    outputFile.close( );

在我的前 2 行代码中,我打印了行数和列数,以便在我回读时使用。 但我无法读取文本文件并再次创建 Mat 对象。

以下是我尝试过的代码。不确定我的代码是否正确。

Mat des_object1;
    std::ifstream file("myFile.txt");
    std::string str; 
    int rows;
    int cols;
    int a = 0;
    while (std::getline(file, str))
    {
        int i = 0;
        int j = 0;

        if(a == 0){
            rows = std::stoi( str );
        }else if(a == 1){
            cols = std::stoi( str );
        }else{

            for(i; i< rows; i++)
            {
                for(j; j<cols; j++)
                {
                     des_object1.at<float>(i,j) = ::atof(str.c_str());
                     break;
                }
            }

        }
        ++a;
    }

【问题讨论】:

    标签: c++ opencv mat


    【解决方案1】:

    使用 opencv FileStorage 可能要容易得多:

    // write:
    Mat m;
    FileStorage fs("myfile.txt",FileStorage::WRITE);
    fs << "mat1" << m;
    
    // read:
    FileStorage fs("myfile.txt",FileStorage::READ);
    fs["mat1"] >> m;
    

    【讨论】:

    • 当我将垫子写入文件时这项工作。但是当我读到它时,给我一个空的
    • @berak,您提供的链接提到 FileStorage 适用于 xml/yaml 文件,没有提及 .txt 文件。我尝试读取 .txt 文件,但它给了我一个错误:解析错误:有效的 xml 应以 icvXMLParse 中的 ''> 开头...... ..
    • 这实际上适用于 txt 文件。尝试将imread 写入m 并使用代码将其写入myfile.txt。如果您有兴趣,请查看输出。然后通过提供的读码回读到m,并用imwrite保存;您会看到它已保存并且图像可见
    【解决方案2】:

    您的内部循环位于错误的位置:您需要在每次迭代读取一行时保留计数器。不过,它可以做得更简单:

    if (in >> rows >> cols) {
        // resize the matrix to its proper size
        for (int r(0); r!= rows; ++r) {
            for (int c(0); c != cols; ++c) {
                if (!(in >> mat[r][c])) {
                    throw std:: runtime_error("failed to read matrix");
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      试试这样的:

      while 循环之外初始化这两个

      int k=0;
      int l=0;
      

      而不是使用for 循环

      if(j<cols){
          des_object1.at<float>(k,l) = ::atof(str.c_str());
      }else{
          k=0;
          l++;
          des_object1.at<float>(k,l) = ::atof(str.c_str());
      }
      j++;
      

      【讨论】:

        【解决方案4】:

        我编写了一个从.asc 读取Mat 的方法,该方法也适用于.txt

        也许有更时尚、更有效的方法可以做到这一点,但这种方法有效且易于理解。

        头部

        int Load_From_Path_Text(Mat *pMA_Out, string path)
        

        变量

        ifstream        IS_File;
        string          ST_Line;
        stringstream    SS_Line;
        unsigned int    rows        = 0;
        unsigned int    cols        = 0;
        unsigned int    y           = 0;
        unsigned int    x           = 0;
        float           F_Value;
        

        获取大小

        IS_File.open(path);
        while(getline(IS_File, ST_Line))
        {
            if(cols == 0)
            {
                SS_Line << ST_Line;
                while(SS_Line >> F_Value)
                    cols++;
            }
        
            rows++;
        }
        IS_File.close();
        

        创建图像

        *pMA_Out = Mat(rows, cols, CV_32FC1);
        

        读取数据

        IS_File.open(path);
        while(getline(IS_File, ST_Line))
        {
            SS_Line.clear();
            SS_Line << ST_Line;
            x = 0;
        
            while(SS_Line >> F_Value)
            {
                pMA_Out->at<float>(y, x) = F_Value;
                x++;
            }
        
            y++;
        }
        IS_File.close();
        

        示例

        1 1 1 -2
        1 2 1 0.5
        1 1 3 2.1
        1 1 1 1.5
        

        转向 this (使用normalize 转换为CV_8UC1 后,将显示CV_MINMAX)。

        【讨论】:

          猜你喜欢
          • 2015-08-14
          • 1970-01-01
          • 2021-02-05
          • 2018-08-08
          • 2018-07-04
          • 1970-01-01
          • 1970-01-01
          • 2020-04-16
          • 1970-01-01
          相关资源
          最近更新 更多