将CvSeq保存到数组中

Ham*_*aya 6 opencv

我在OpenCV文档中有点迷失,我想将cvFindContours返回的CvSeq保存到一个数组中,据我所知,它将返回一个CvContour的seq但我找不到它包含的内容?它应该保存到哪个部分以后我可以稍后迭代它并说出调用cvBoundingRect等.

car*_*eri 8

CvContour是一个与CvSeq具有相同字段的结构,还有一些其他结构,其中最重要的是CvRect rect(请参阅include/opencv/cxtypes.h).所以它真的归结为CvSeq是什么.

有一个名为opencv.pdf的文件,它附带OpenCV源,并在p.138(对于OpenCV 2.1)它说CvSeq定义如下:

#define CV_SEQUENCE\_FIELDS()
    int flags; /* micsellaneous flags */ \
    int header_size; /* size of sequence header */ \
    struct CvSeq* h_prev; /* previous sequence */ \
    struct CvSeq* h_next; /* next sequence */ \
    struct CvSeq* v_prev; /* 2nd previous sequence */ \
    struct CvSeq* v_next; /* 2nd next sequence */ \
    int total; /* total number of elements */ \
    int elem_size;/* size of sequence element in bytes */ \
    char* block_max;/* maximal bound of the last block */ \
    char* ptr; /* current write pointer */ \
    int delta_elems; /* how many elements allocated when the sequence grows
    (sequence granularity) */ \
    CvMemStorage* storage; /* where the seq is stored */ \
    CvSeqBlock* free_blocks; /* free blocks list */ \
    CvSeqBlock* first; /* pointer to the first sequence block */

typedef struct CvSeq
{
    CV_SEQUENCE_FIELDS()
} CvSeq;
Run Code Online (Sandbox Code Playgroud)

假设您像这样调用cvFindContours:

cvFindContours(img, storage, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
Run Code Online (Sandbox Code Playgroud)

,contours调用后将指向第一个轮廓cvFindContours.如果你想得到它的边界矩形,你只需将它传递给cvBoundingRect.可以访问序列中的下一个轮廓contours->h_next.在轮廓树的情况下,也就是说,当轮廓可以在图像中的另一个轮廓内时,您可以访问当前轮廓的第一个内轮廓contours->v_next.下一个内部轮廓(如果存在)将是contours->v_next->h_next,等等.

如果要将序列转换为可以使用的数组cvCvtSeqToArray.

你也可以使用从OpenCV 2.0开始的C++接口,这似乎更好用.例如,CvSeq** contours参数cvFindContours变为vector<vector<Point> >& contours.