Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read from an XML-string in OpenCV?

Tags:

c++

xml

opencv

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?

like image 496
mr_georg Avatar asked Oct 04 '12 12:10

mr_georg


People also ask

How do I read an XML file from another XML file?

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 ().

How do I create an XML/YAML data structure in OpenCV?

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.

How do I open an XML file in Python?

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.

How do I serialize OpenCV data in Python?

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.


1 Answers

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;
like image 183
Andrey Kamaev Avatar answered Sep 20 '22 19:09

Andrey Kamaev