Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to load matrix from text file(.txt) as float values

Hi.. I am doing a project on action recognition in which I have to load values in a text file which are in double type to a matrix in the double format in the Opencv c++.. I tried using FileStorage but could not load the .txt files.. Please help me in this regard..

I tried this code.

include <iostream> // for cout, getline

include <sstream> // for istringstream

include <string> // for string

include <opencv2\highgui\highgui.hpp>

include <fstream>

using namespace std; using namespace cv; int main() { std::ifstream file("E:\op.txt"); // assume the file opens correctly

double matrix[500][10];
int row = 0, col = 0;

std::string line;
while (std::getline(file, line)) {  // read each line from the file
    // okay we have a line, let's extract the numbers on this line
    // this is an "input string stream", a stream formed from a string
    std::istringstream stream(line);

    double x;
    col = 0;  // reset column counter
    while (stream >> x) {  // keep trying to read ints until there are no more
        matrix[row][col] = x;
        col++;
    }

    // okay that's all the data on this line, go to the next line
    row++;
}

// now let's print out the data we read into the matrix.
// At this point, row tells us how many rows there are, and
// we never reset col so col still tells us how many values were on
// the last line (let's hope this is the same as the other rows of the
// matrix).
for (int i = 0; i < row; i++) {
    for (int j = 0; j < col; j++) {
        std::cout << matrix[i][j];
    }
    std::cout << "\n";
    cout << typeid(matrix).name() << endl;

}
system("pause >nul");
return 0;

}

how to load matrix from text file(.txt) as float values

Hi.. I am doing a project on action recognition in which I have to load values in a text file which are in double type to a matrix in the double format in the Opencv c++.. I tried using FileStorage but could not load the .txt files.. Please help me in this regard..

I tried this code.

include
#include <iostream>  // for cout, getline

include getline #include <sstream> // for istringstream

include istringstream #include <string> // for string

include <opencv2\highgui\highgui.hpp>

include <fstream>

string #include <opencv2\highgui\highgui.hpp> #include <fstream> using namespace std; using namespace cv; int main() { std::ifstream file("E:\op.txt"); // assume the file opens correctly

correctly

    double matrix[500][10];
 int row = 0, col = 0;

 std::string line;
 while (std::getline(file, line)) {  // read each line from the file
     // okay we have a line, let's extract the numbers on this line
     // this is an "input string stream", a stream formed from a string
     std::istringstream stream(line);

     double x;
     col = 0;  // reset column counter
     while (stream >> x) {  // keep trying to read ints until there are no more
         matrix[row][col] = x;
         col++;
     }

     // okay that's all the data on this line, go to the next line
     row++;
 }

 // now let's print out the data we read into the matrix.
 // At this point, row tells us how many rows there are, and
 // we never reset col so col still tells us how many values were on
 // the last line (let's hope this is the same as the other rows of the
 // matrix).
 for (int i = 0; i < row; i++) {
     for (int j = 0; j < col; j++) {
         std::cout << matrix[i][j];
     }
     std::cout << "\n";
     cout << typeid(matrix).name() << endl;

 }
 system("pause >nul");
 return 0;
}

}