58939e47afd0b26672e73f1c29e632d5a3e0669f
[platform/upstream/opencv.git] / samples / c / contours.c
1 #include "opencv2/imgproc/imgproc_c.h"
2 #include "opencv2/highgui/highgui_c.h"
3 #include <stdio.h>
4
5 static void help(void)
6 {
7     printf("\nThis program creates an image to demonstrate the use of the \"c\" contour\n"
8             "functions: cvFindContours() and cvApproxPoly() along with the storage\n"
9             "functions cvCreateMemStorage() and cvDrawContours().\n"
10             "It also shows the use of a trackbar to control contour retrieval.\n"
11             "\n"
12             "Usage :\n"
13             "./contours\n");
14 }
15
16 #define w 500
17 int levels = 3;
18 CvSeq* contours = 0;
19
20 static void on_trackbar(int pos)
21 {
22     IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
23     CvSeq* _contours = contours;
24     int _levels = levels - 3;
25     (void)pos;
26
27     if( _levels <= 0 ) // get to the nearest face to make it look more funny
28         _contours = _contours->h_next->h_next->h_next;
29     cvZero( cnt_img );
30     cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
31     cvShowImage( "contours", cnt_img );
32     cvReleaseImage( &cnt_img );
33 }
34
35 static void findCComp( IplImage* img )
36 {
37     int x, y, cidx = 1;
38     IplImage* mask = cvCreateImage( cvSize(img->width+2, img->height+2), 8, 1 );
39     cvZero(mask);
40     cvRectangle( mask, cvPoint(0, 0), cvPoint(mask->width-1, mask->height-1),
41                  cvScalarAll(1), 1, 8, 0 );
42
43     for( y = 0; y < img->height; y++ )
44         for( x = 0; x < img->width; x++ )
45         {
46             if( CV_IMAGE_ELEM(mask, uchar, y+1, x+1) != 0 )
47                 continue;
48             cvFloodFill(img, cvPoint(x,y), cvScalarAll(cidx),
49                         cvScalarAll(0), cvScalarAll(0), 0, 4, mask);
50             cidx++;
51         }
52 }
53
54
55 int main(int argc, char* argv[])
56 {
57     int i, j;
58     CvMemStorage* storage = cvCreateMemStorage(0);
59     IplImage* img = cvCreateImage( cvSize(w,w), 8, 1 );
60     IplImage* img32f = cvCreateImage( cvSize(w,w), IPL_DEPTH_32F, 1 );
61     IplImage* img32s = cvCreateImage( cvSize(w,w), IPL_DEPTH_32S, 1 );
62     IplImage* img3 = cvCreateImage( cvSize(w,w), 8, 3 );
63     (void)argc; (void)argv;
64
65     help();
66     cvZero( img );
67
68     for( i=0; i < 6; i++ )
69     {
70         int dx = (i%2)*250 - 30;
71         int dy = (i/2)*150;
72         CvScalar white = cvRealScalar(255);
73         CvScalar black = cvRealScalar(0);
74
75         if( i == 0 )
76         {
77             for( j = 0; j <= 10; j++ )
78             {
79                 double angle = (j+5)*CV_PI/21;
80                 cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),
81                     cvRound(dy+100-90*sin(angle))),
82                     cvPoint(cvRound(dx+100+j*10-30*cos(angle)),
83                     cvRound(dy+100-30*sin(angle))), white, 3, 8, 0);
84             }
85         }
86
87         cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
88         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
89         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
90         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
91         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
92         cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
93         cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
94         cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
95         cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
96         cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
97         cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
98     }
99
100     cvNamedWindow( "image", 1 );
101     cvShowImage( "image", img );
102     cvConvert( img, img32f );
103     findCComp( img32f );
104     cvConvert( img32f, img32s );
105
106     cvFindContours( img32s, storage, &contours, sizeof(CvContour),
107                     CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
108
109     //cvFindContours( img, storage, &contours, sizeof(CvContour),
110     //                CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
111
112
113     {
114     const char* attrs[] = {"recursive", "1", 0};
115     cvSave("contours.xml", contours, 0, 0, cvAttrList(attrs, 0));
116     contours = (CvSeq*)cvLoad("contours.xml", storage, 0, 0);
117     }
118
119     // comment this out if you do not want approximation
120     contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
121
122     cvNamedWindow( "contours", 1 );
123     cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
124
125     {
126         CvRNG rng = cvRNG(-1);
127
128         CvSeq* tcontours = contours;
129         cvCvtColor( img, img3, CV_GRAY2BGR );
130         while( tcontours->h_next )
131             tcontours = tcontours->h_next;
132
133         for( ; tcontours != 0; tcontours = tcontours->h_prev )
134         {
135             CvScalar color;
136             color.val[0] = cvRandInt(&rng) % 256;
137             color.val[1] = cvRandInt(&rng) % 256;
138             color.val[2] = cvRandInt(&rng) % 256;
139             color.val[3] = cvRandInt(&rng) % 256;
140             cvDrawContours(img3, tcontours, color, color, 0, -1, 8, cvPoint(0,0));
141             if( tcontours->v_next )
142             {
143                 color.val[0] = cvRandInt(&rng) % 256;
144                 color.val[1] = cvRandInt(&rng) % 256;
145                 color.val[2] = cvRandInt(&rng) % 256;
146                 color.val[3] = cvRandInt(&rng) % 256;
147                 cvDrawContours(img3, tcontours->v_next, color, color, 1, -1, 8, cvPoint(0,0));
148             }
149         }
150
151     }
152
153     cvShowImage( "colored", img3 );
154     on_trackbar(0);
155     cvWaitKey(0);
156     cvReleaseMemStorage( &storage );
157     cvReleaseImage( &img );
158     cvReleaseImage( &img32f );
159     cvReleaseImage( &img32s );
160     cvReleaseImage( &img3 );
161
162     return 0;
163 }
164
165 #ifdef _EiC
166 main(1,"");
167 #endif