Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save and read from XML the new C++ style matrix objects in OpenCV?

Tags:

xml

opencv

matrix

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.

like image 374
neuviemeporte Avatar asked Apr 12 '10 11:04

neuviemeporte


1 Answers

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)

like image 161
Laxit Gavshinde Avatar answered Sep 22 '22 13:09

Laxit Gavshinde