Opencv:无法使用OpenCV从文本文件中正确读/写? [英] Opencv: Unable to correctly read/write from/to using OpenCV from a text file?

查看:156
本文介绍了Opencv:无法使用OpenCV从文本文件中正确读/写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统信息:windows 7,32 bit,opencv 2.4.10,msvs 2010



我有一个文本文件有一些整数值。
我想在Mat对象m中读取这些值,然后在控制台上打印出来。



到目前为止,我尝试的是:

 
{

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

if(!fs.isOpened()){std :: cout< 无法打开文件存储! << std :: endl; return 0;}

fs [mat1]>> m;

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



return 0;

}

但这会打印;


mat1 = []


文件中的实际数据是: / p>

  123456 
123456

$ b b

请帮助我理解这里有什么错误。



更新
我甚至尝试mytext.xml,只需重命名.text文件。但我仍然看到空矩阵如上所示。



因此,文件不打开,因为输出是:


打开文件存储!

FileStorage 无法读取任意txt文件。它需要是xml或yml。因此,如果你想以这种方式阅读它,你最好也保存它之前的方式。

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






文件,尝试不同的方法:



(这是假设整数!)

  #include< fstream> 
using namespace std;

ifstream in(my.txt);
vector< int> nums;
while(!in.eof()){
int n;
在>> n;
nums.push_back(n);
}
//现在从向量中创建一个Mat:
Mat mat(nums);


System info: windows 7, 32 bit, opencv 2.4.10, msvs 2010

I have a text file having some integer values. I want to read these values in a Mat object m, and then print out on the console.

What I tried so far is this:

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 this prints ;

mat1 = []

the actual data in the file is:

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 empty matrix as the out above.

Oh so the file is not opening as the output is:

unable to open file storage!

解决方案

the FileStorage cannot read arbitrary txt files. it needs to be either xml or yml. so, if you want to read it in that way, you better save it that way before, too.

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


if all you have is a txt file, try a different approach:

(this assumes integers !)

#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);

这篇关于Opencv:无法使用OpenCV从文本文件中正确读/写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆