6 function ret = randint(a, b)
7 ret = int32(rand() * (b - a) + a);
10 function minarea_array(img, count)
13 pointMat = cvCreateMat( count, 1, cv.CV_32SC2 );
15 pointMat(i) = cvPoint( randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4) );
18 box = cvMinAreaRect2( pointMat );
19 box_vtx = cvBoxPoints( box );
20 [success, center, radius] = cvMinEnclosingCircle( pointMat );
23 cvCircle( img, cvGet1D(pointMat,i), 2, CV_RGB( 255, 0, 0 ), \
24 cv.CV_FILLED, cv.CV_AA, 0 );
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 ) ;
36 function minarea_seq(img, count, storage)
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 );
42 pt0 = cvPoint( randint(img.width/4, img.width*3/4), randint(img.height/4, img.height*3/4) );
43 cvSeqPush( ptseq, pt0 );
45 box = cvMinAreaRect2( ptseq );
46 box_vtx = cvBoxPoints( box );
47 [success, center, radius] = cvMinEnclosingCircle( ptseq );
49 for pt = CvSeq_map(ptseq),
51 cvCircle( img, pt, 2, CV_RGB( 255, 0, 0 ), cv.CV_FILLED, cv.CV_AA, 0 );
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 );
63 img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
64 storage = cvCreateMemStorage(0);
66 cvNamedWindow( "rect & circle", 1 );
71 count = randint(1,100);
73 minarea_seq(img, count, storage);
75 minarea_array(img, count);
78 cvShowImage("rect & circle", img);