Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / samples / c / convexhull.c
1 #ifdef _CH_
2 #pragma package <opencv>
3 #endif
4
5 #ifndef _EiC
6 #include "cv.h"
7 #include "highgui.h"
8 #include <stdlib.h>
9 #endif
10
11 #define ARRAY  1
12
13 int main( int argc, char** argv )
14 {
15     IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
16 #if !ARRAY        
17     CvMemStorage* storage = cvCreateMemStorage(0);
18 #endif
19
20     cvNamedWindow( "hull", 1 );
21         
22     for(;;)
23     {
24         char key;
25         int i, count = rand()%100 + 1, hullcount;
26         CvPoint pt0;
27 #if !ARRAY            
28         CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
29                                      sizeof(CvPoint), storage );
30         CvSeq* hull;
31     
32         for( i = 0; i < count; i++ )
33         {
34             pt0.x = rand() % (img->width/2) + img->width/4;
35             pt0.y = rand() % (img->height/2) + img->height/4;
36             cvSeqPush( ptseq, &pt0 );
37         }
38         hull = cvConvexHull2( ptseq, 0, CV_CLOCKWISE, 0 );
39         hullcount = hull->total;
40 #else
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 );
45
46         for( i = 0; i < count; i++ )
47         {
48             pt0.x = rand() % (img->width/2) + img->width/4;
49             pt0.y = rand() % (img->height/2) + img->height/4;
50             points[i] = pt0;
51         }
52         cvConvexHull2( &pointMat, &hullMat, CV_CLOCKWISE, 0 );
53         hullcount = hullMat.cols;
54 #endif
55         cvZero( img );
56         for( i = 0; i < count; i++ )
57         {
58 #if !ARRAY                
59             pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
60 #else
61             pt0 = points[i];
62 #endif
63             cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
64         }
65
66 #if !ARRAY            
67         pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );
68 #else
69         pt0 = points[hull[hullcount-1]];
70 #endif
71
72         for( i = 0; i < hullcount; i++ )
73         {
74 #if !ARRAY                
75             CvPoint pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
76 #else
77             CvPoint pt = points[hull[i]];
78 #endif
79             cvLine( img, pt0, pt, CV_RGB( 0, 255, 0 ), 1, CV_AA, 0 );
80             pt0 = pt;
81         }
82
83         cvShowImage( "hull", img );
84
85         key = (char) cvWaitKey(0);
86         if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
87             break;
88
89 #if !ARRAY
90         cvClearMemStorage( storage );
91 #else
92         free( points );
93         free( hull );
94 #endif
95     }
96     
97     cvDestroyWindow( "hull" );
98     return 0;
99 }
100
101 #ifdef _EiC
102 main(1,"convexhull.c");
103 #endif
104