Node iterator missing first value when importing xml [closed]

asked 2015-09-03 06:30:29 -0600

albertJ gravatar image

updated 2015-09-03 06:31:15 -0600

I'm currently saving a large map of histogram data to xml for use in a later part of my program. I've noticed that it is cutting off the first value when loading and instead adding an empty value at the end.

A basic example is:

Correct Model:
<model1>0,1,1,1,1,1,1,0</model1>

Incorrect Loaded Model:
<model1>1,1,1,1,1,1,0,1.4013e-45</model1>

The layout of my xml file below where i is a changing value:

<Serial></Serial>
<classes>
  <class_i>
  <Name></Name>
    <m_i type_id="opencv-matrix">
    </m_i>
  </class_i>
</classes>

My import function is below:

int getClassHist(map<string, vector<Mat> >& savedClassHist){
  int serial;
  // Load in Class Histograms(Models)
  FileStorage fs3("models.xml", FileStorage::READ);
  fs3["Serial"] >> serial;
  cout << "This is the serial: " << serial << endl;
  FileNode fn = fs3["classes"];
  if(fn.type() == FileNode::MAP){

    // Create iterator to go through all the classes
    for(FileNodeIterator it = fn.begin();it != fn.end();it++){
      string clsNme = (string)(*it)["Name"];
      savedClassHist[clsNme];

      // Create node of current Class
      FileNode clss = (*it)["Models"];
      // Iterate through each model inside class, saving to map
      for(FileNodeIterator it1  = clss.begin();it1 != clss.end();it1++){
        FileNode k = *it1;
        Mat tmp;
        k >> tmp;
        savedClassHist[clsNme].push_back(tmp);
      }
    }
    fs3.release();
  }else{
    ERR("Class file was not map.");
    exit(-1);
  }
  return serial;
}

It seems like an iterator problem so would be likely be in the it1 loop which is skipping the first value but i'm not sure why.??

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by albertJ
close date 2015-09-05 10:51:25.916666

Comments

Could you provide a sample .xml file?

LorenaGdL gravatar imageLorenaGdL ( 2015-09-03 08:53:09 -0600 )edit

Hi yeah here's one of them

albertJ gravatar imagealbertJ ( 2015-09-03 09:20:34 -0600 )edit
1

It seems to work perfectly to me. Why do you know it is loading wrong values? I mean, how do you access savedClassHist later?

LorenaGdL gravatar imageLorenaGdL ( 2015-09-03 11:04:52 -0600 )edit

very good point, i had incorrectly written the printing function and it was starting from an incorrect position. Thank you for your help, my mistake

albertJ gravatar imagealbertJ ( 2015-09-05 10:50:50 -0600 )edit

Pleasure ;)

LorenaGdL gravatar imageLorenaGdL ( 2015-09-05 11:26:06 -0600 )edit