Save CvSeq in an array

I lost a little in OpenCV docs, I would like to save the CvSeq returned by cvFindContours to an array, from which I understand that it will return seq CvContour s, but I could not find what it contains? what parts of this should I save for this later, I can later scroll through it and say call cvBoundingRect, etc.

+6
opencv
source share
1 answer

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

There is an opencv.pdf file that comes with OpenCV sources and on page 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; 

Let's say you call cvFindContours as follows:

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

where contours will point to the first contour after calling cvFindContours . If you want to get your bounding box, you just pass it to cvBoundingRect . The next contour in the sequence can be obtained through contours->h_next . In the case of circuits, i.e. When the path can be inside another image path, you can access the first inner path of the current path through contours->v_next . The next inner contour, if it exists, will be contours->v_next->h_next , etc.

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

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

+8
source share

All Articles