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;
}
What happens? What should happen? How does your file look like?
far better than allocating a fixed size matrix[100][10] would be:
cv::Mat img;
img.push_back(x);
(like with a vector)img = img.reshape(1, cols);
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...
Start by adding info requested by the people above
If you will just keep repeating your remarks, that won't help a lot.
after reshape i want display matrix as image , how to do that?