This works for me for reading .txt files. Please include the relevant header files.
//To read data from a text file.
//filename is the name of the text file
//rows and cols show dimensions of the matrix written in the text file
#include"stdafx.h"
#include<fstream>
using namespace std;
using namespace cv;
Mat ReadMatFromTxt(string filename, int rows,int cols)
{
double m;
Mat out = Mat::zeros(rows, cols, CV_64FC1);//Matrix to store values
ifstream fileStream(filename);
int cnt = 0;//index starts from 0
while (fileStream >> m)
{
int temprow = cnt / cols;
int tempcol = cnt % cols;
out.at<double>(temprow, tempcol) = m;
cnt++;
}
return out;
}
However, you must know the dimensions of the matrix beforehand. Any improvement is appreciated.
you asked the same thing yesterday. please spare us duplicates here.
all the data in the txt file is being loaded as a single column .please help me in separating cols.
if you know the intended column size, it's a simple
img=img.reshape(1,cols);
thanks berak , the cols in the reshape function is separting rows.. can you help me in separating frame values in a text file for each frame in a matrix.
i don't understand you.
Example: text file {1 5 0.666 0.535......2 0.24 0.82... 3 .042 .0287...} in this example the values 1 2 3 are frame numbers. expected output: {1 5 0.666 0.535........ 2 0.24 0.82......... 3 0.042 .0287.......} each row is one video frame.