Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / octave / minarea.m
1 #! /usr/bin/env octave
2
3 cv;
4 highgui;
5
6 function ret = randint(a, b)
7   ret = int32(rand() * (b - a) + a);
8 endfunction
9
10 function minarea_array(img, count)
11   global cv;
12   global highgui;
13   pointMat = cvCreateMat( count, 1, cv.CV_32SC2 );
14   for i=0:count-1,
15     pointMat(i) = cvPoint( randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4) );
16   endfor
17
18   box = cvMinAreaRect2( pointMat );
19   box_vtx = cvBoxPoints( box );
20   [success, center, radius] = cvMinEnclosingCircle( pointMat );
21   cv.cvZero( img );
22   for i=0:count-1,
23     cvCircle( img, cvGet1D(pointMat,i), 2, CV_RGB( 255, 0, 0 ), \
24              cv.CV_FILLED, cv.CV_AA, 0 );
25   endfor
26
27   box_vtx = {cvPointFrom32f(box_vtx{1}), \
28              cvPointFrom32f(box_vtx{2}), \
29              cvPointFrom32f(box_vtx{3}), \
30              cvPointFrom32f(box_vtx{4})};
31   cvCircle( img, cvPointFrom32f(center), cvRound(radius), CV_RGB(255, 255, 0), 1, cv.CV_AA, 0 );
32   cvPolyLine( img, {box_vtx}, 1, CV_RGB(0,255,255), 1, cv.CV_AA ) ;
33 endfunction
34
35
36 function minarea_seq(img, count, storage)
37   global cv;
38   global highgui;
39   ptseq = cvCreateSeq( bitor(cv.CV_SEQ_KIND_GENERIC, cv.CV_32SC2), cv.sizeof_CvContour, cv.sizeof_CvPoint, storage );
40   ptseq = cv.CvSeq_CvPoint.cast( ptseq );
41   for i=0:count-1,
42     pt0 = cvPoint( randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4) );
43     cvSeqPush( ptseq, pt0 );
44   endfor
45   box = cvMinAreaRect2( ptseq );
46   box_vtx = cvBoxPoints( box );
47   [success, center, radius] = cvMinEnclosingCircle( ptseq );
48   cv.cvZero( img );
49   for pt = CvSeq_map(ptseq),
50     pt = pt{1};
51     cvCircle( img, pt, 2, CV_RGB( 255, 0, 0 ), cv.CV_FILLED, cv.CV_AA, 0 );
52   endfor
53
54   box_vtx = {cvPointFrom32f(box_vtx{1}), \
55              cvPointFrom32f(box_vtx{2}), \
56              cvPointFrom32f(box_vtx{3}), \
57              cvPointFrom32f(box_vtx{4})};
58   cvCircle( img, cvPointFrom32f(center), cvRound(radius), CV_RGB(255, 255, 0), 1, cv.CV_AA, 0 );
59   cvPolyLine( img, {box_vtx}, 1, CV_RGB(0,255,255), 1, cv.CV_AA );
60   cvClearMemStorage( storage );
61 endfunction
62
63 img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
64 storage = cvCreateMemStorage(0);
65
66 cvNamedWindow( "rect & circle", 1 );
67
68 use_seq=false;
69
70 while (true),
71   count = randint(1,100);
72   if (use_seq)
73     minarea_seq(img, count, storage);
74   else
75     minarea_array(img, count);
76   endif
77
78   cvShowImage("rect & circle", img);
79   key = cvWaitKey();
80   if( key == '\x1b' );
81     break;
82   endif
83
84   use_seq = !use_seq;
85 endwhile