CvContour 是一个与 CvSeq 具有相同字段的结构体,还有一些其他字段,其中最重要的是 CvRect 矩形(请参阅 include/opencv/cxtypes.h)。所以这实际上可以归结为 CvSeq 是什么。
有一个文件叫opencv.pdf随 OpenCV 源一起提供,并在 p.1 中。 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;
假设您像这样调用 cvFindContours:
cvFindContours(img, storage, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
, where contours
调用后将指向第一个轮廓cvFindContours
。如果你想得到它的边界矩形,你只需将它传递给cvBoundingRect
。序列中的下一个轮廓可以通过以下方式访问contours->h_next
。对于轮廓树,即当图像中的一个轮廓可以位于另一个轮廓内部时,您可以通过以下方式访问当前轮廓的第一个内部轮廓contours->v_next
。下一个内部轮廓(如果存在)将是contours->v_next->h_next
,等等。
如果你想将序列转换为数组,你可以使用cvCvtSeqToArray http://opencv.willowgarage.com/documentation/c/core_dynamic_structures.html?highlight=cvcvtseqtoarray#cvCvtSeqToArray.
从 OpenCV 2.0 开始,您还可以使用 C++ 接口,这似乎更好用。例如,CvSeq** contours
参数为cvFindContours
变成vector<vector<Point> >& contours
.