13

I know how to load/save a cv::Mat instance into a XML-file (See this question).

But what I really need, is to parse a std::string (or char *) that contains the XML, and get the cv::Mat. Say I get the XML out of a database, and not from a file.

Is that possible?

Community
  • 1
  • 1
mr_georg
  • 3,635
  • 5
  • 35
  • 52

1 Answers1

19

You can do it since OpenCV 2.4.1.

Here is a code sample from release notes:

//==== storing data ====
FileStorage fs(".xml", FileStorage::WRITE + FileStorage::MEMORY);
fs << "date" << date_string << "mymatrix" << mymatrix;
string buf = fs.releaseAndGetString();

//==== reading it back ====
FileStorage fs(buf, FileStorage::READ + FileStorage::MEMORY);
fs["date"] >> date_string;
fs["mymatrix"] >> mymatrix;
Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • Wonderful. Anyone who met the same problems, You should not write things like FileStorage::READ||FileStorage::MEMORY like other libs - you should use + instead. – xxbidiao May 05 '14 at 07:20
  • While reading it back, how do you specify the format (YAML/XML)? I ask because I'm writing it as .xml. – rdasxy Oct 23 '14 at 22:22
  • @rdasxy, the format is detected automatically. I don't know for sure but most likely it decides based on XML/YML declaration tag – Andrey Kamaev Oct 24 '14 at 09:34
  • 4
    @xxbidiao you shouldn't use `+`, what you need is logical OR `|` instead of boolean OR `||` – Darien Pardinas Jul 20 '16 at 12:19