Ask Your Question
0

how to read matrix from text file to Mat in opencv

asked 2014-09-24 02:22:38 -0600

SAKHILETI SATYA KISHORE gravatar image

Hi.. In my projec, I have to load matrix from a text file to MAt object in opencv. . Please help me in this regard.. eg 1 2 3 4 5 6 7 8 9 in text file. it should be loded to Mat

edit retag flag offensive close merge delete

Comments

you asked the same thing yesterday. please spare us duplicates here.

berak gravatar imageberak ( 2014-09-24 02:36:00 -0600 )edit

all the data in the txt file is being loaded as a single column .please help me in separating cols.

SAKHILETI SATYA KISHORE gravatar imageSAKHILETI SATYA KISHORE ( 2014-09-24 02:55:33 -0600 )edit

if you know the intended column size, it's a simple img=img.reshape(1,cols);

berak gravatar imageberak ( 2014-09-24 02:58:17 -0600 )edit

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.

SAKHILETI SATYA KISHORE gravatar imageSAKHILETI SATYA KISHORE ( 2014-09-24 03:16:59 -0600 )edit

i don't understand you.

berak gravatar imageberak ( 2014-09-24 03:18:07 -0600 )edit

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.

SAKHILETI SATYA KISHORE gravatar imageSAKHILETI SATYA KISHORE ( 2014-09-24 03:27:25 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-08-17 02:41:51 -0600

Ruchir gravatar image

updated 2015-08-17 02:42:34 -0600

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.

edit flag offensive delete link more

Comments

THnx a lot. If i want to read file which has number like 0.0425098 0.0456149 , i want to know what type of CV_xxX do i have to use.

davidkim gravatar imagedavidkim ( 2016-06-29 17:26:42 -0600 )edit
0

answered 2014-09-24 06:22:10 -0600

FileStorage fs("test.xml", FileStorage::WRITE);
Mat myMatrix = (Mat_<int>(3,3) << 1,2,3,4,5,6,7,8,9);
fs << "myMatrix" << myMatrix;
fs.release();

Mat myMatrix2;
FileStorage fs2("test.xml", FileStorage::READ);
fs2["myMatrix"] >> myMatrix2;
fs2.release();

Content of "test.xml":

 <?xml version="1.0"?>
 <opencv_storage>
 <myMatrix type_id="opencv-matrix">
   <rows>3</rows>
   <cols>3</cols>
   <dt>i</dt>
   <data>
     1 2 3 4 5 6 7 8 9</data></myMatrix>
 </opencv_storage>

http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html#

edit flag offensive delete link more

Comments

'FileStorage' is able to handle .xml and .yml files. my file is .txt from which I've to load the float values.

SAKHILETI SATYA KISHORE gravatar imageSAKHILETI SATYA KISHORE ( 2014-09-25 05:19:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-09-24 02:22:38 -0600

Seen: 16,647 times

Last updated: Aug 17 '15