2 #pragma package <opencv>
13 int main( int argc, char** argv )
15 IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
17 CvMemStorage* storage = cvCreateMemStorage(0);
20 cvNamedWindow( "hull", 1 );
25 int i, count = rand()%100 + 1, hullcount;
28 CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
29 sizeof(CvPoint), storage );
32 for( i = 0; i < count; i++ )
34 pt0.x = rand() % (img->width/2) + img->width/4;
35 pt0.y = rand() % (img->height/2) + img->height/4;
36 cvSeqPush( ptseq, &pt0 );
38 hull = cvConvexHull2( ptseq, 0, CV_CLOCKWISE, 0 );
39 hullcount = hull->total;
41 CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
42 int* hull = (int*)malloc( count * sizeof(hull[0]));
43 CvMat pointMat = cvMat( 1, count, CV_32SC2, points );
44 CvMat hullMat = cvMat( 1, count, CV_32SC1, hull );
46 for( i = 0; i < count; i++ )
48 pt0.x = rand() % (img->width/2) + img->width/4;
49 pt0.y = rand() % (img->height/2) + img->height/4;
52 cvConvexHull2( &pointMat, &hullMat, CV_CLOCKWISE, 0 );
53 hullcount = hullMat.cols;
56 for( i = 0; i < count; i++ )
59 pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
63 cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
67 pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );
69 pt0 = points[hull[hullcount-1]];
72 for( i = 0; i < hullcount; i++ )
75 CvPoint pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
77 CvPoint pt = points[hull[i]];
79 cvLine( img, pt0, pt, CV_RGB( 0, 255, 0 ), 1, CV_AA, 0 );
83 cvShowImage( "hull", img );
85 key = (char) cvWaitKey(0);
86 if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
90 cvClearMemStorage( storage );
97 cvDestroyWindow( "hull" );
102 main(1,"convexhull.c");