Opencv: Can't read / write correctly from / to using OpenCV from a text file?

System Information: Windows 7, 32 bit, opencv 2.4.10, msvs 2010

I have a text file with integer values. I want to read these values ​​in a Mat m object and then print to the console.

What I have tried so far is the following:

int main()
 {

Mat m;
/// read:
 FileStorage fs("myfile.txt",FileStorage::READ);

if (!fs.isOpened()) {std::cout << "unable to open file storage!" << std::endl; return 0;}

fs["mat1"] >> m;

cout << "mat1 = "<< endl << " "  << m << endl << endl;



return 0;

}

However, it prints;

mat1 = []

actual data in the file:

123456
123456

So kindly help me understand what is wrong here.

Update I even tried mytext.xml by simply renaming the .text file. But still I see the empty matrix as above.

So the file does not open because the output is:

unable to open file storage!

0
source share
2 answers

FileStorage txt. xml, yml. , , .

Mat m = ...;
/// write:
FileStorage fs("myfile.txt",FileStorage::WRITE);
fs << "mat1" << m;
fs.release(); // flush

, , txt , :

( !)

#include <fstream>
using namespace std;

ifstream in("my.txt");
vector<int> nums;
while ( !in.eof() ) {
    int n;
    in >> n;
    nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat(nums);
+1

, FileStorage , ,

fs["mat1"] >> m;

mat1 - , .yml .xml.

-1

All Articles