Ask Your Question
1

How to read a Matrix from a child node with FileStorage API

asked 2015-07-12 10:48:00 -0600

valentine gravatar image

updated 2015-08-25 17:21:04 -0600

Hello guys,

I am having a little issue here. I saved a couple of mat as child nodes in xml using the Filestorage API in opencv. However when i try to read them back the filestorage API reads the same values which are wrong for all the Matrices in the xml file. I don't know where am getting it wrong. please help.

Here is my code for writing and reading.

FileStorage file(filename, FileStorage::WRITE);
file << "Features" << "{";
for (int i = 0; i < TrainFeatureVector.size(); i++){
  string element1 = "f" + to_string(i);
  file << element1 << TrainFeatureVector[i];
}
file << "}"

FileStorage fs(filename, FileStorage::READ);
Mat feat;
string element;
string element1;
FileNode k = fs["Features"];
for (int i = 0; i < k.size(); i++){
 element1 = "f" + to_string(i);
 k[element1] >> feat;
 Fvector.push_back(feat);}

Here is my xml file C:\fakepath\sxml.png

edit retag flag offensive close merge delete

Comments

May be you should try something like k=fs["f0"] or k=fs["f1"]

LBerger gravatar imageLBerger ( 2015-07-12 11:07:53 -0600 )edit

Tried using FileNodeIterator but only returns one Matrix. Please need help on this.

valentine gravatar imagevalentine ( 2015-07-12 12:15:27 -0600 )edit

TrainFeatureVector is a vector<mat> ?

berak gravatar imageberak ( 2015-07-13 00:08:44 -0600 )edit

@berak, yes

valentine gravatar imagevalentine ( 2015-07-14 12:12:50 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
1

answered 2015-07-16 12:13:54 -0600

valentine gravatar image

Hello everyone.

Here is what i did to resolve this issue.

void readfeaturesFileNodeList(const FileNode& fn, vector<Mat>& result, int i){
    string element1;
    Mat feat;
    for (FileNodeIterator current = fn.begin(); current != fn.end();current++){
        element1 = "f" + to_string(i);
        FileNode rt = *current;
        rt[element1]>> feat;
        result.push_back(feat);
    }
}

and the second function is given below.

void ReadGlobalFeatures(string filename,vector<Mat>& Fvector){
    FileStorage fs(filename, FileStorage::READ);
    Mat feat;
    vector<Mat> vfeat;
    string element;
    string element1;
    FileNode k = fs.root();

    for (int i = 0; i <TrainLabels.size(); i++){
        //element1 = "f" + to_string(i);
        FileNode tempID = k;
        readfeaturesFileNodeList(tempID, vfeat,i);
    }
    Fvector = vfeat;
    fs.release();
}

Thanks berak and all who contributed to this i am really grateful. This is truly the best opencv forum and i have learnt from all you.

edit flag offensive delete link more
1

answered 2015-07-12 12:31:31 -0600

LBerger gravatar image

updated 2015-07-12 12:43:15 -0600

Ok i was wrong I have looked in my code and I have found :

cv::FileNode op = node;
nomOperation=(string)op["op"];
nbOperande= op["nbOperande"];
indOpFenetre.resize(nbOperande);
this->op.resize(nbOperande);
indRes = (int)op["res"];
indEtape = (int)op["indEtape"];
idOperation = (int)op["idOperation"];
nomSequence = (string)op["nomSequence"];
cv::FileNode opl = op["intParam"];
cv::FileNodeIterator it = opl.begin(), it_end = opl.end();
for (; it != it_end; ++it)
 {
   intParam[(string)(*it)["nom"]] = DomaineParametreOp<int>((*it)["valeur"], (*it)["minVal"], (*it)["maxVal"], (*it)["pasVal"]);;
}

for file like this

image description

edit flag offensive delete link more
1

answered 2015-07-14 12:35:09 -0600

berak gravatar image

maybe it gets easier, if you just write/read a vector/list of Mat's, and don't try to make it a 'dictionary'. your f1,f2,f3 keys are quite artificial here.

vector<Mat> TrainFeatureVector(4,Mat::eye(4,4,CV_32F)); // just for demo purpose
FileStorage fsw("ma.xml", FileStorage::WRITE);
fsw << "Features" << "[";
for (int i = 0; i < TrainFeatureVector.size(); i++)
{
    fsw << TrainFeatureVector[i];
}
fsw << "]";
fsw.release();


vector<Mat> TrainFeatureVector2;
FileStorage fsr("ma.xml", FileStorage::READ);
FileNode k = fsr["Features"];
for (FileNodeIterator it=k.begin(); it!=k.end(); ++it)
{
    Mat feature;
    (*it) >> feature;
    TrainFeatureVector2.push_back(feature);
}
cerr << TrainFeatureVector2[2];

[1, 0, 0, 0;
 0, 1, 0, 0;
 0, 0, 1, 0;
 0, 0, 0, 1]

 <?xml version="1.0"?>
<opencv_storage>
<Features>
  <_ type_id="opencv-matrix">
    <rows>4</rows>
    <cols>4</cols>
    <dt>f</dt>
    <data>
      1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1.</data></_>
  <_ type_id="opencv-matrix">
    <rows>4</rows>
    <cols>4</cols>
    <dt>f</dt>
    <data>
      1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1.</data></_>
  <_ type_id="opencv-matrix">
    <rows>4</rows>
    <cols>4</cols>
    <dt>f</dt>
    <data>
      1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1.</data></_>
  <_ type_id="opencv-matrix">
    <rows>4</rows>
    <cols>4</cols>
    <dt>f</dt>
    <data>
      1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1.</data></_></Features>
</opencv_storage>
edit flag offensive delete link more

Comments

Dear berak,

Thanks for answer. done exactly this but its reading wrong values. The values am getting its wat in xml file

valentine gravatar imagevalentine ( 2015-07-14 15:40:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-12 10:48:00 -0600

Seen: 2,032 times

Last updated: Jul 16 '15