converted some more samples to C++
[profile/ivi/opencv.git] / samples / c / contours.c
1 #include <opencv2/imgproc/imgproc.hpp>
2 #include <opencv2/highgui/highgui.hpp>
3
4 #define w 500
5 int levels = 3;
6 CvSeq* contours = 0;
7
8 void on_trackbar(int pos)
9 {
10     IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
11     CvSeq* _contours = contours;
12     int _levels = levels - 3;
13     if( _levels <= 0 ) // get to the nearest face to make it look more funny
14         _contours = _contours->h_next->h_next->h_next;
15     cvZero( cnt_img );
16     cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
17     cvShowImage( "contours", cnt_img );
18     cvReleaseImage( &cnt_img );
19 }
20
21 int main( int argc, char** argv )
22 {
23     int i, j;
24     CvMemStorage* storage = cvCreateMemStorage(0);
25     IplImage* img = cvCreateImage( cvSize(w,w), 8, 1 );
26
27     cvZero( img );
28
29     for( i=0; i < 6; i++ )
30     {
31         int dx = (i%2)*250 - 30;
32         int dy = (i/2)*150;
33         CvScalar white = cvRealScalar(255);
34         CvScalar black = cvRealScalar(0);
35
36         if( i == 0 )
37         {
38             for( j = 0; j <= 10; j++ )
39             {
40                 double angle = (j+5)*CV_PI/21;
41                 cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),
42                     cvRound(dy+100-90*sin(angle))),
43                     cvPoint(cvRound(dx+100+j*10-30*cos(angle)),
44                     cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
45             }
46         }
47
48         cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
49         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
50         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
51         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
52         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
53         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
54         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
55         cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
56         cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
57         cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
58         cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
59     }
60
61     cvNamedWindow( "image", 1 );
62     cvShowImage( "image", img );
63
64     cvFindContours( img, storage, &contours, sizeof(CvContour),
65                     CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
66
67     // comment this out if you do not want approximation
68     contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
69
70     cvNamedWindow( "contours", 1 );
71     cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
72
73     on_trackbar(0);
74     cvWaitKey(0);
75     cvReleaseMemStorage( &storage );
76     cvReleaseImage( &img );
77
78     return 0;
79 }
80
81 #ifdef _EiC
82 main(1,"");
83 #endif