Riddhiman Dasgupta
Riddhiman Dasgupta

Reputation: 383

XML Parser in OpenCV

I have a file locations.xml which contains image filenames, and positions of rectangles drawn on the corresponding image. The XML node structure is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<tagset>
  <image>
    <imageName>ryoungt_05.08.2002/aPICT0034.JPG</imageName>
    <resolution x="960" y="1280" />
    <taggedRectangles>
      <taggedRectangle x="196.0" y="901.0" width="111.0" height="67.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="116.0" y="896.0" width="59.0" height="69.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="442.0" y="794.0" width="424.0" height="67.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="212.0" y="793.0" width="200.0" height="66.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="99.0" y="560.0" width="302.0" height="76.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="107.0" y="791.0" width="84.0" height="66.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="512.0" y="682.0" width="366.0" height="74.0" offset="0.0" rotation="0.0" userName="admin" />
      <taggedRectangle x="104.0" y="678.0" width="376.0" height="73.0" offset="0.0" rotation="0.0" userName="admin" />
    </taggedRectangles>
  </image>

I need to open this file in OpenCV and read it such that for every image filename in the XML file, the corresponding image will be opened in a window, and the rectangles will be drawn on the corresponding image.

Basically I need to open these files and see the rectangles to match them with rectangles drawn on the same images using a text detection algorithm. But it's handling the XML files that have me stumped. Any help is appreciated.

Upvotes: 4

Views: 2331

Answers (3)

stackAccountsAreAPITA
stackAccountsAreAPITA

Reputation: 21

There is a hacky way to parse an XML file with OpenCV without the <opencv_storage> tags in a pinch, which is to read the XML file into a string, add the <opencv_storage> tag pair, and then open the string with FileStorage in the FileStorage::MEMORY mode, kinda like:

std::ifstream stm("foo.xml");
std::stringstream buffer;
buffer << "<opencv_storage>\n"<<stm.rdbuf()<<"</opencv_storage>\n";
string fileContent =buffer.str();
FileStorage fs(fileContent, FileStorage::READ|FileStorage::MEMORY);

It has limitations, obviously, but easier than pulling in XML reader dependency sometimes.

Upvotes: 0

antoine
antoine

Reputation: 1893

OpenCV has some (de-)serialization tool which support XML, YAML and JSON file format

You can deserialize your XML into OpenCV objects directly. Take a look at the example here.

But this is only useful when OpenCV is involved on both sides. XML parser requires the <opencv_storage> tag.

Upvotes: 2

Jakub M.
Jakub M.

Reputation: 33857

OpenCV is OpenCV and XML is XML. One has not much to do with the other.

Check this thread

What is the best open XML parser for C++?

I personally used pugixml and I was content

Upvotes: 2

Related Questions