开发者

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

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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜