Ask Your Question

How to save and load Matx from a file?

asked 2015-05-21 13:27:46 -0500

larry37 gravatar image

updated 2015-05-22 09:27:02 -0500

When trying to save a cv::Matx to a file using FileStorage, openCV throws some errors which say saving a Matx is not yet implemented. A workaround is to cast the Matx to a cv::Mat when writing:

cv::FileStorage fs("file.xml", cv::FileStorage::WRITE);
cv::Matx33d x;
fs<<"X"<<cv::Mat(x); // file contains x

However, when loading a cv::Matx from file, simply casting it to cv::Mat does not work. I assume the result is not copied, and the Matx is left uninitialized:

cv::FileStorage fs("file.xml", cv::FileStorage::READ);
cv::Matx33d x;
fs["X"]>>cv::Mat(x); // x contains gibberish

Is there a simple workaround for this, without resorting to creating temporary cv::Mat's and casting them to Matx after reading?

edit retag flag offensive close merge delete


Have you try something like x=(cv::Mat)fs(["X"]);

LBerger gravatar imageLBerger ( 2015-05-21 15:01:52 -0500 )edit

Using x=(cv::Mat)(fs["X"]) produces the error "cannot convert from 'cv::FileNode' to 'cv::Mat' "

larry37 gravatar imagelarry37 ( 2015-05-22 09:25:52 -0500 )edit

even if you use a temporary Mat, you have to invert the casting order:

cv::FileStorage fs2("file.xml", cv::FileStorage::READ);
Mat tmp;
fs2["X"] >> tmp; 
cv::Matx33d y(tmp); // all fine.
berak gravatar imageberak ( 2015-05-22 09:52:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-09-14 02:35:21 -0500

RajKumar gravatar image

The above problem can be solved by doing the following:

cv::FileStorage fs2("file.xml", cv::FileStorage::READ); Mat tmp;

fs2["X"] >> tmp; //in C/C++ fs2.Write(tmp,"X"); // in C#

fs2.release(); //in C/C++ fs2.Dispose(); //in C# cv::Matx33d y(tmp); // all fine

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-05-21 13:27:46 -0500

Seen: 1,190 times

Last updated: Sep 14 '16