2cff7b111ddf5b2978bd40930a9d5a7325eebd38
[opencv] / samples / c / minarea.c
1 #ifdef _CH_
2 #pragma package <opencv>
3 #endif
4
5 #define CV_NO_BACKWARD_COMPATIBILITY
6
7 #ifndef _EiC
8 #include "cv.h"
9 #include "highgui.h"
10 #include <stdlib.h>
11 #endif
12
13 #define ARRAY  1
14
15 int main( int argc, char** argv )
16 {
17     IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
18 #if !ARRAY
19     CvMemStorage* storage = cvCreateMemStorage(0);
20 #endif
21
22     cvNamedWindow( "rect & circle", 1 );
23
24     for(;;)
25     {
26         char key;
27         int i, count = rand()%100 + 1;
28         CvPoint pt0, pt;
29         CvBox2D box;
30         CvPoint2D32f box_vtx[4];
31         CvPoint2D32f center;
32         CvPoint icenter;
33         float radius;
34 #if !ARRAY
35         CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
36                                      sizeof(CvPoint), storage );
37         for( i = 0; i < count; i++ )
38         {
39             pt0.x = rand() % (img->width/2) + img->width/4;
40             pt0.y = rand() % (img->height/2) + img->height/4;
41             cvSeqPush( ptseq, &pt0 );
42         }
43 #ifndef _EiC /* unfortunately, here EiC crashes */
44         box = cvMinAreaRect2( ptseq, 0 );
45 #endif
46         cvMinEnclosingCircle( ptseq, &center, &radius );
47 #else
48         CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
49         CvMat pointMat = cvMat( 1, count, CV_32SC2, points );
50
51         for( i = 0; i < count; i++ )
52         {
53             pt0.x = rand() % (img->width/2) + img->width/4;
54             pt0.y = rand() % (img->height/2) + img->height/4;
55             points[i] = pt0;
56         }
57 #ifndef _EiC
58         box = cvMinAreaRect2( &pointMat, 0 );
59 #endif
60         cvMinEnclosingCircle( &pointMat, &center, &radius );
61 #endif
62         cvBoxPoints( box, box_vtx );
63         cvZero( img );
64         for( i = 0; i < count; i++ )
65         {
66 #if !ARRAY
67             pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
68 #else
69             pt0 = points[i];
70 #endif
71             cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
72         }
73
74 #ifndef _EiC
75         pt0.x = cvRound(box_vtx[3].x);
76         pt0.y = cvRound(box_vtx[3].y);
77         for( i = 0; i < 4; i++ )
78         {
79             pt.x = cvRound(box_vtx[i].x);
80             pt.y = cvRound(box_vtx[i].y);
81             cvLine(img, pt0, pt, CV_RGB(0, 255, 0), 1, CV_AA, 0);
82             pt0 = pt;
83         }
84 #endif
85         icenter.x = cvRound(center.x);
86         icenter.y = cvRound(center.y);
87         cvCircle( img, icenter, cvRound(radius), CV_RGB(255, 255, 0), 1, CV_AA, 0 );
88
89         cvShowImage( "rect & circle", img );
90
91         key = (char) cvWaitKey(0);
92         if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
93             break;
94
95 #if !ARRAY
96         cvClearMemStorage( storage );
97 #else
98         free( points );
99 #endif
100     }
101
102     cvDestroyWindow( "rect & circle" );
103     return 0;
104 }
105
106 #ifdef _EiC
107 main(1,"convexhull.c");
108 #endif
109