在opencv 1.0中打开yml文件

时间:2011-10-12 13:17:51

标签: c++ opencv

我有一个yml文件,我想使用现有的opencv 1.0函数打开文件进行读取。该文件包含以下内容:

%YAML:1.0
Image file: "00961010.jpg"
Contours count: 8
Contours:
  -
  Name: FO
  Count: 41
  Closed: 0
  Points:
     -
        x: 740.7766113281250000
        y: 853.0124511718750000
     -
        x: 745.1353149414062500
        y: 875.5324096679687500

请举例说明如何迭代这些数据?我只需要x,y点然后存储在数组中。我搜索过,但我没有找到类似的数据示例,请帮助我。提前致谢!

1 个答案:

答案 0 :(得分:0)

您将要查看cvFileStorage数据结构和函数。

以下是OpenCV的一个示例,以帮助您入门:

#include "cxcore.h"

int main( int argc, char** argv )
{
    CvFileStorage* fs = cvOpenFileStorage( "points.yml", 0, CV_STORAGE_READ );
    CvStringHashNode* x_key = cvGetHashedNode( fs, "x", -1, 1 );
    CvStringHashNode* y_key = cvGetHashedNode( fs, "y", -1, 1 );
    CvFileNode* points = cvGetFileNodeByName( fs, 0, "points" );

    if( CV_NODE_IS_SEQ(points->tag) )
    {
        CvSeq* seq = points->data.seq;
        int i, total = seq->total;
        CvSeqReader reader;
        cvStartReadSeq( seq, &reader, 0 );
        for( i = 0; i < total; i++ )
        {
            CvFileNode* pt = (CvFileNode*)reader.ptr;

            int x = cvReadIntByName( fs, pt, "x", 0 /* default value */ );
            int y = cvReadIntByName( fs, pt, "y", 0 /* default value */ );

            CV_NEXT_SEQ_ELEM( seq->elem_size, reader );
            printf("point%d is (x = %d, y = %d)\n", i, x, y);
        }
    }
    cvReleaseFileStorage( &fs );
    return 0;
}