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

asked 2014-09-23 01:22:18 -0600

SAKHILETI SATYA KISHORE gravatar image

updated 2014-09-23 02:03:41 -0600

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;
}
edit retag flag offensive close merge delete

Comments

2

What happens? What should happen? How does your file look like?

FooBar gravatar imageFooBar ( 2014-09-23 01:52:37 -0600 )edit
4

far better than allocating a fixed size matrix[100][10] would be:

  • start with an empty cv::Mat img;
  • whenever you've read your x, do: img.push_back(x); (like with a vector)
  • when you're done reading, count the rows/cols, do a: img = img.reshape(1, cols);
berak gravatar imageberak ( 2014-09-23 01:53:34 -0600 )edit

i am getting feature points stored in a .txt file.. I should read the float values in .txt file to a matrix in the same format..I should give the matrix as input to an algorithm...

SAKHILETI SATYA KISHORE gravatar imageSAKHILETI SATYA KISHORE ( 2014-09-23 02:40:39 -0600 )edit
1

Start by adding info requested by the people above

  • Add an example of your data file
  • Try switching to the approach berak suggested

If you will just keep repeating your remarks, that won't help a lot.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-09-23 04:14:25 -0600 )edit

after reshape i want display matrix as image , how to do that?

sumit patel gravatar imagesumit patel ( 2016-02-19 01:18:20 -0600 )edit