Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / c / example_cmake / minarea.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( "rect & circle", 1 );
21         
22     for(;;)
23     {
24         char key;
25         int i, count = rand()%100 + 1;
26         CvPoint pt0, pt;
27         CvBox2D box;
28         CvPoint2D32f box_vtx[4];
29         CvPoint2D32f center;
30         CvPoint icenter;
31         float radius;
32 #if !ARRAY            
33         CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
34                                      sizeof(CvPoint), storage );
35         for( i = 0; i < count; i++ )
36         {
37             pt0.x = rand() % (img->width/2) + img->width/4;
38             pt0.y = rand() % (img->height/2) + img->height/4;
39             cvSeqPush( ptseq, &pt0 );
40         }
41 #ifndef _EiC /* unfortunately, here EiC crashes */
42         box = cvMinAreaRect2( ptseq, 0 );
43 #endif
44         cvMinEnclosingCircle( ptseq, &center, &radius );
45 #else
46         CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
47         CvMat pointMat = cvMat( 1, count, CV_32SC2, points );
48
49         for( i = 0; i < count; i++ )
50         {
51             pt0.x = rand() % (img->width/2) + img->width/4;
52             pt0.y = rand() % (img->height/2) + img->height/4;
53             points[i] = pt0;
54         }
55 #ifndef _EiC
56         box = cvMinAreaRect2( &pointMat, 0 );
57 #endif
58         cvMinEnclosingCircle( &pointMat, &center, &radius );
59 #endif
60         cvBoxPoints( box, box_vtx );
61         cvZero( img );
62         for( i = 0; i < count; i++ )
63         {
64 #if !ARRAY                
65             pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
66 #else
67             pt0 = points[i];
68 #endif
69             cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
70         }
71
72 #ifndef _EiC
73         pt0.x = cvRound(box_vtx[3].x);
74         pt0.y = cvRound(box_vtx[3].y);
75         for( i = 0; i < 4; i++ )
76         {
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);
80             pt0 = pt;
81         }
82 #endif
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 );
86
87         cvShowImage( "rect & circle", img );
88
89         key = (char) cvWaitKey(0);
90         if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
91             break;
92
93 #if !ARRAY
94         cvClearMemStorage( storage );
95 #else
96         free( points );
97 #endif
98     }
99     
100     cvDestroyWindow( "rect & circle" );
101     return 0;
102 }
103
104 #ifdef _EiC
105 main(1,"convexhull.c");
106 #endif
107