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( "rect & circle", 1 );
25 int i, count = rand()%100 + 1;
28 CvPoint2D32f box_vtx[4];
33 CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
34 sizeof(CvPoint), storage );
35 for( i = 0; i < count; i++ )
37 pt0.x = rand() % (img->width/2) + img->width/4;
38 pt0.y = rand() % (img->height/2) + img->height/4;
39 cvSeqPush( ptseq, &pt0 );
41 #ifndef _EiC /* unfortunately, here EiC crashes */
42 box = cvMinAreaRect2( ptseq, 0 );
44 cvMinEnclosingCircle( ptseq, ¢er, &radius );
46 CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
47 CvMat pointMat = cvMat( 1, count, CV_32SC2, points );
49 for( i = 0; i < count; i++ )
51 pt0.x = rand() % (img->width/2) + img->width/4;
52 pt0.y = rand() % (img->height/2) + img->height/4;
56 box = cvMinAreaRect2( &pointMat, 0 );
58 cvMinEnclosingCircle( &pointMat, ¢er, &radius );
60 cvBoxPoints( box, box_vtx );
62 for( i = 0; i < count; i++ )
65 pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
69 cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
73 pt0.x = cvRound(box_vtx[3].x);
74 pt0.y = cvRound(box_vtx[3].y);
75 for( i = 0; i < 4; i++ )
77 pt.x = cvRound(box_vtx[i].x);
78 pt.y = cvRound(box_vtx[i].y);
79 cvLine(img, pt0, pt, CV_RGB(0, 255, 0), 1, CV_AA, 0);
83 icenter.x = cvRound(center.x);
84 icenter.y = cvRound(center.y);
85 cvCircle( img, icenter, cvRound(radius), CV_RGB(255, 255, 0), 1, CV_AA, 0 );
87 cvShowImage( "rect & circle", img );
89 key = (char) cvWaitKey(0);
90 if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
94 cvClearMemStorage( storage );
100 cvDestroyWindow( "rect & circle" );
105 main(1,"convexhull.c");