如何在OpenCV中保存和读取新的c++风格的矩阵对象?

时间:2023-01-16 15:43:44

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.

旧的、C风格的cvMat矩阵可以传递给cvSave()函数,以便轻松地编写XML文件。新的c++风格的cv::Mat和cv::Mat_矩阵不被这个函数接受。

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.

OpenCV引用有一个关于XML持久性的章节,但是这三个类(文件存储、文件名和FileNodeIterator)没有任何描述或示例,我无法从接口中找到如何使用它们。

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.

编辑:这实际上涉及OpenCV的c++界面中的许多其他功能,如2.1版。文档在某些地方非常糟糕,函数参数不一致,用户组要么不知道,要么有比回答问题更好的事情要做。我将继续使用旧的C接口。医生们比我好多了,更不用说奥莱利的书了。

2 个解决方案

#1


15  

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

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

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在文件中创建文件存储变量,然后以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;

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.

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

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

opencv c++ cheatsheet(与文档PDF中的cheatsheet不同)

#2


9  

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

上面的内容是正确的,但是cheatsheet没有显示需要打开文件。这似乎是显而易见的,但我忽略了做这件事,因为作弊者没有说我必须这样做。下面的代码将允许您正确地写入文件

---------- 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.

没关系,这仍然行不通。很抱歉浪费了邮件。

#1


15  

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

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

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在文件中创建文件存储变量,然后以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;

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.

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

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

opencv c++ cheatsheet(与文档PDF中的cheatsheet不同)

#2


9  

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

上面的内容是正确的,但是cheatsheet没有显示需要打开文件。这似乎是显而易见的,但我忽略了做这件事,因为作弊者没有说我必须这样做。下面的代码将允许您正确地写入文件

---------- 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.

没关系,这仍然行不通。很抱歉浪费了邮件。