Retrieving Points of Polygons created by cvApproxPoly()
I have a CvSeq* containing the polygons created by running cvApproxPoly() on a CvSeq* created from running cvFindContors on a b&w input image. I want to access the CvPoints of each polygon returned from the CvSeq*.开发者_运维技巧 Code so far as follows (outlines is an IplImage containing the b&w input image):
//create pointers to store data we're going to be calculating
CvMemStorage* storage = cvCreateMemStorage();
CvSeq* first_contour = NULL;
CvSeq* first_polygon = NULL;
//find the contours (edges) of the silhouette, in terms of pixels.
cvFindContours( &outlines,
storage,
&first_contour,
sizeof(CvContour),
CV_RETR_LIST );
//convert the pixel contours to line segments in a polygon.
first_polygon = cvApproxPoly(first_contour,
sizeof(CvContour),
storage,
CV_POLY_APPROX_DP,
2,
1);
I can use cvDrawContour to draw the polygon onto an image, but I want to iterate over each 2D point that defines each contour. It looks like each element of the CvSeq* first_polygon contains the set of points for a single polygon (concluded based on the value of first_polygon->total;, but I don't know how to access the individual points. Help please?
You can use cvGetSeqElem to iterate over the vertices of the polygon. squares.c in samples/c has implemented this thing.
Resolved this myself: CvSeq* is multi-dimensional. e.g: cvSeq->total; is the first dimension (each polygon), eg cvSeq->first->total is the second dimension (each point in a polygon).
精彩评论