将CvSeq保存到数组 [英] Save CvSeq to an array

查看:168
本文介绍了将CvSeq保存到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenCV文档中有些失落,我想将cvFindContours返回的CvSeq保存到数组,据我所知它将返回CvContour的seq,但我找不到它包含的内容?以后我应该保存其中的哪些部分,以后再进行迭代并说调用cvBoundingRect等.

I am a bit lost in OpenCV docs, I would like to save CvSeq returned by cvFindContours to an array, from what I understand it will return a seq of CvContour s but i couldn't find what it contains? which parts of it should I save to that later I can later I can iterate through it and say call cvBoundingRect etc.

推荐答案

CvContour是具有与CvSeq相同字段的结构,另外还有一些其他字段,其中最重要的是CvRect rect(请参见include/opencv/cxtypes.h).因此,实际上可以归结为CvSeq是什么.

A CvContour is a struct with the same fields as a CvSeq, plus a few others, the most important of which is a CvRect rect (see include/opencv/cxtypes.h). So it really boils down to what a CvSeq is.

OpenCV源代码附带一个名为 opencv.pdf 的文件,位于p中. 138(对于OpenCV 2.1)表示CvSeq定义如下:

There is a file called opencv.pdf that comes with the OpenCV sources, and in p. 138 (for OpenCV 2.1) it says that CvSeq is defined as follows:

#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;

假设您这样调用cvFindContours:

Let's say you call cvFindContours like this:

cvFindContours(img, storage, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

,其中contours将在调用cvFindContours后指向第一个轮廓.如果要获取其边界矩形,只需将其传递给cvBoundingRect.序列中的下一个轮廓可以通过contours->h_next访问.对于轮廓树,也就是说,当轮廓可以位于图像中的另一个轮廓内部时,可以通过contours->v_next访问当前轮廓的第一个内部轮廓.下一个内部轮廓(如果存在)将是contours->v_next->h_next,依此类推.

, where contours will be pointing to the first contour after calling cvFindContours. If you want to get its bounding rectangle, you simply pass it to cvBoundingRect. The next contour in the sequence can be accessed through contours->h_next. In the case of contour trees, that is, when a contour can be inside another contour in the image, you can access the first inner contour of the current contour through contours->v_next. The next inner contour, if it exists, would be contours->v_next->h_next, and so forth.

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

If you want to convert a sequence to an array you can use cvCvtSeqToArray.

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

You can also use the C++ interface starting from OpenCV 2.0 which seems to be nicer to use. For instance, the CvSeq** contours parameter to cvFindContours becomes vector<vector<Point> >& contours.

这篇关于将CvSeq保存到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆