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?
Reading XML Files To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree.parse () method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot ().
The XML/YAML data structure in OpenCV is cv::FileStorage . To specify that this structure to which file binds on your hard drive you can use either its constructor or the open () function of this: Either one of this you use the second argument is a constant specifying the type of operations you'll be able to on them: WRITE, READ or APPEND.
Elementtree library. For the purpose of reading and writing the xml file we would be using a Python library named BeautifulSoup. In order to install the library, type the following command into the terminal. Beautiful Soup supports the HTML parser included in Python’s standard library, but it also supports a number of third-party Python parsers.
In C++, it's possible to serialize this through the OpenCV I/O XML/YAML interface (just as in case of the OpenCV data structures) by adding a read and a write function inside and outside of your class. In Python, you can get close to this by implementing a read and write function inside the class.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With