如何保存和读取 XML 新的 C++ OpenCV 中的样式矩阵对象?

发布于 2024-08-29 06:07:08 字数 465 浏览 4 评论 0原文

旧的 C 风格 cvMat 矩阵可以传递给 cvSave() 函数,以便轻松写入 XML 文件。此函数不接受新的 C++ 样式 cv::Matcv::Mat_ 矩阵。

OpenCV 参考中有一个关于 XML 持久性的部分,但是这三个类(FileStorage、FileNode 和 FileNodeIterator)缺乏任何描述或示例,我无法弄清楚如何从界面使用它们。

谢谢。

编辑:这实际上涉及OpenCV的新C++界面中的许多其他功能,从版本2.1开始。文档在某些地方非常糟糕,函数参数不一致,用户组要么不知道,要么有比回答问题更好的事情要做。我将坚持使用旧的 C 界面一段时间。这些文档好多了,更不用说 O'Reilly 的书了。

The old, C style cvMat matrices could be passed to the cvSave() function for easy writing to an XML file. The new C++ style cv::Mat and cv::Mat_ matrices are not accepted by this function.

The OpenCV reference has a section on XML persistence, but the three classes (FileStorage, FileNode and FileNodeIterator) lack any description or example and I can't figure out how to use them from the interface.

Thanks.

EDIT: This actually concerns a lot of other functionality in the new C++ interface of OpenCV, as of Version 2.1. The documentation is very poor in places, the function arguments are inconsistent, and the user group either has no idea, or has better things to do than answer questions. I'm going to stick to the old C interface for a while. The docs are tons better, not to mention the book by O'Reilly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2024-09-05 06:07:08

显然,C++ 风格更容易,但正如您所说,没有任何容易获得的文档。

要在文件中写入 cv::Mat ,只需创建一个 FileStorage 变量,然后按照您使用 cout 在屏幕上打印的样式写入矩阵。

cv::Mat someMatrix;
//you create and assign values to someMatrix however you plan to.
FileStorage fs("myFile.yml", FileStorage::WRITE);
fs << "name_to_identify_matrix_by" << someMatrix;

阅读方式也与cin风格类似,但最好看一下下面的链接以便更好地理解。在数据 I/O 部分的第二页上,他们展示了如何使用 XML/YAML 的示例。

opencv C++ 备忘单(与文档 PDF 中的备忘单不同) )

Apparently its easier in C++ style, but as you said there aren't any easily available documentation.

To Write cv::Mat in a file just create a FileStorage variable and then write the matrix in the style you use cout to print on screen.

cv::Mat someMatrix;
//you create and assign values to someMatrix however you plan to.
FileStorage fs("myFile.yml", FileStorage::WRITE);
fs << "name_to_identify_matrix_by" << someMatrix;

Reading is also similar to cin style, but its better you take a look at the below link to have a better understanding. On 2nd page in section Data I/O they have shown examples on how to use XML/YAML.

opencv C++ cheatsheet(different than cheatsheet in the documentation PDF)

三岁铭 2024-09-05 06:07:08

上面是正确的,但是备忘单没有显示您需要打开该文件。这似乎是显而易见的,但我忽略了这样做,因为备忘单没有说我必须这样做。这是允许您正确写入文件的代码

---------- 代码:

// write Mat objects to the freakin file
FileStorage fs("CamModel.yml", FileStorage::WRITE);
if (!fs.isOpened()){
 fs.open("CamModel.yml", FileStorage::WRITE);
 fs << "mtxCam" << cameraMatrix;
 fs << "mtxDist" << distCoeffs;
 fs.release();
}

// to test that it really worked, read the Mats back in
if (!fs.isOpened()){
 fs.open("CamModel.yml", FileStorage::READ);
 fs["mtxCam"] >> cameraMatrix;
 fs["mtxDist"] >> distCoeffs;
 fs.release();
}

没关系,这仍然不起作用。抱歉浪费了帖子。

The above is correct, but what the cheatsheet does not show is that you need to open the file. This may seem obvious, but I neglected to do it because the cheatsheet didn't say I had to. here is the code that will allow you to write to the files correctly

---------- code:

// write Mat objects to the freakin file
FileStorage fs("CamModel.yml", FileStorage::WRITE);
if (!fs.isOpened()){
 fs.open("CamModel.yml", FileStorage::WRITE);
 fs << "mtxCam" << cameraMatrix;
 fs << "mtxDist" << distCoeffs;
 fs.release();
}

// to test that it really worked, read the Mats back in
if (!fs.isOpened()){
 fs.open("CamModel.yml", FileStorage::READ);
 fs["mtxCam"] >> cameraMatrix;
 fs["mtxDist"] >> distCoeffs;
 fs.release();
}

Nevermind, this still doesn't work. sorry for the wasted post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文