4

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.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
Riddhiman Dasgupta
  • 363
  • 1
  • 6
  • 22
  • if it's your xml file, you could switch from json to xml and save parsing time. because json parsing is much simpler and faster than xml. :) – Evgen Bodunov Feb 11 '12 at 11:41
  • 1
    JSON is not an option. Because all the files are not mine. And I do not know anything about JSON yet. And I'll have to figure out how ti incorporate the JSON parser results into OpenCV function arguments. – Riddhiman Dasgupta Feb 11 '12 at 15:31

3 Answers3

2

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.

antoine
  • 1,653
  • 11
  • 23
2

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

Community
  • 1
  • 1
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
  • 1
    Yes, I've thought of using TinyXML or something more full-fledged, but do I need to use a different parser for my rather simple needs? Is there no way to do it using the inbuilt XML/YAML handling functions in OpenCV? – Riddhiman Dasgupta Feb 11 '12 at 15:30
0

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.