下面是一个有点集序列或数组创建凸边形的简单示例
#include<cv.h>
#include<highgui.h>
#include<stdlib.h>
#define ARRAY 0
int main()
{
IplImage* img = cvCreateImage (cvSize(500, 500), 8, 3);
cvNamedWindow ("hull", 1);
#if ! ARRAY
CvMemStorage* storage = cvCreateMemStorage (0);
#endif
int i;
int contour = rand() % 100 + 1;
int hullcontour;
CvPoint pt0;
#if ! ARRAY
CvSeq* ptseq = cvCreateSeq (CV_SEQ_KIND_GENERIC | CV_32SC2, sizeof(CvContour), sizeof(CvPoint), storage);
CvSeq* hull;
for (i = 0; i < contour; i++)
{
pt0.x = rand() % (img->width / 2) + img->width / 4;
pt0.y = rand() % (img->height / 2) + img->height / 4;
cvSeqPush (ptseq, &pt0);
}
hull = cvConvexHull2 (ptseq, 0, CV_CLOCKWISE, 0);
/*hull = cvConvexHull2 (ptseq, 0, CV_CLOCKWISE, 1);*/
hullcontour = hull->total;
#else
CvPoint* points = (CvPoint*)malloc(contour * sizeof(points[0]));
int* hull = (int*)malloc(contour * sizeof(hull[0]));
CvMat point_mat = cvMat (1, contour, CV_32SC2, points);
CvMat hull_mat = cvMat (1, contour, CV_32SC1, hull);
for (i = 0; i < contour; i+