tags:

views:

322

answers:

2

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.

+1  A: 

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)

Laxit Gavshinde
A: 

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.

NotNamedDwayne
yeah ... I was totally wrong when I posted this before. the line FileStorage fs("CamModel.yml", FileStorage::WRITE); does open the file. My problem was that the file name that I was trying to open was unopenable due to a string of characters that were being attached to the beginning of my file name. the instructions given in the cheetsheet are of course correct.
NotNamedDwayne