Ask Your Question
0

load matrix from values in text file

asked 2016-06-10 02:51:02 -0600

I've written a matrix (rows: 238 and columns 51) in a text file using MATLAB. In text file, the columns are separated by tab and each line in the text fie corresponds to a row of that matrix. Now, I want to read that file into a matrix in OpenCV. How can I do it?

I've read a number of posts where they were using "stdafx.h" header file in their code but I can't add it to my code because I think that I don't have this header file. I can't understand also. So please help me out here.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-06-10 03:13:21 -0600

berak gravatar image

updated 2016-06-10 03:15:04 -0600

you can use plain c++ std lib for this:

#include <fstream>

std::ifstream in("my.txt");
// you did not tell us, what type of data to read, i'll assume float here !
cv::Mat m; // initially empty
// slurp in data, one at a time
for  (int i=0; i<238*51; i++)
{
    float f;
    in >> f;
    m.push_back(f);
}
m = m.reshape(1, 238); // finally, reshape to 51x238
edit flag offensive delete link more

Comments

@berak please help me here link text

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-06-16 02:16:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-10 02:51:02 -0600

Seen: 910 times

Last updated: Jun 10 '16