Filestorage and vector

asked 2015-03-09 16:18:37 -0600

LBerger gravatar image

Hi, I have read http://docs.opencv.org/doc/tutorials/... about Filestorage. Now I want to save in a yml file image and contour. When I open yml file I 've got image stat and stag :

image description and after contour with only one contour :

image description

I have check data in regular file and i can see

image description

My program :

int main(int ac, char** av) 
{
Mat m,mThresh,mCmp;
Mat statMom,statG;
Mat imCtr;
vector<vector<cv::Point> >pointCtr;
vector<cv::Vec4i> arbo;

m=imread("C:\\tmp\\small.tif",cv::IMREAD_UNCHANGED);
threshold( m, mThresh, 50,255,THRESH_BINARY);
connectedComponentsWithStats(mThresh, mCmp,statMom,statG,8, CV_32S);
mCmp.copyTo(imCtr);
findContours(imCtr, pointCtr,arbo, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE, cv::Point(0,0));

cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
fs<<"Image"<<mCmp;
fs<<"Stat"<<statMom;
fs<<"Pos"<<statG;
fs<<"Ctr"<<"["<<pointCtr<<"]";
fs<<"Arbo"<<"["<<arbo<<"]";
fs.release();

ofstream gFic("test.txt");
vector<vector<cv::Point> >::iterator itCtr;
gFic<<"**********POINT***************"<<endl;
int indCtr=0;
for (itCtr=pointCtr.begin();itCtr!=pointCtr.end();itCtr++)
    {
    vector<cv::Point> ::iterator itPt;
    gFic<<"Contour "<<indCtr<<" ";
    for (itPt=itCtr->begin();itPt!=itCtr->end();itPt++)
        {
        gFic<<itPt->x<<" "<<itPt->y<<" ";
        }
    gFic<<endl;
    indCtr++;
    }
vector<cv::Vec4i>::iterator ita;
gFic<<"**********ARBO***************"<<endl;
for (ita=arbo.begin();ita!=arbo.end();ita++)
    {
    gFic<<*ita<<" ";
    }
gFic<<endl;


}

and my data : image description

I don't understand why I cannot see in yml file contour point like in my regular file. Thanks you for your help

edit retag flag offensive close merge delete

Comments

1

Finally I have missed some lines in doc. I think it's not possible to write a vector<vector> in FileStorage. With this new code only one vector is written and everything is OK in yml file

cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
fs<<"Image"<<mCmp;
fs<<"Stat"<<statMom;
fs<<"Pos"<<statG;
vector<vector<cv::Point> >::iterator itCtr;
int indCtr=0;
for (itCtr=pointCtr.begin();itCtr!=pointCtr.end();itCtr++)
    {
    stringstream nombre;
    nombre<<indCtr;
    string s("Ctr"+nombre.str());

    fs<<s<<*itCtr;
    indCtr++;
    }    
fs<<"Arbo"<<"["<<arbo<<"]";
fs.release();
LBerger gravatar imageLBerger ( 2015-03-10 03:42:55 -0600 )edit

To bad i didnt see this earlier. Indeed FileStorage only allows writing up to the vector structure. A wrapped structure around it should be manually unwrapped before writing to a FileStorage element.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-10 05:34:54 -0600 )edit