1 #include "opencv2/imgproc/imgproc_c.h"
2 #include "opencv2/highgui/highgui_c.h"
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"
20 static void on_trackbar(int pos)
22 IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
23 CvSeq* _contours = contours;
24 int _levels = levels - 3;
27 if( _levels <= 0 ) // get to the nearest face to make it look more funny
28 _contours = _contours->h_next->h_next->h_next;
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 );
35 static void findCComp( IplImage* img )
38 IplImage* mask = cvCreateImage( cvSize(img->width+2, img->height+2), 8, 1 );
40 cvRectangle( mask, cvPoint(0, 0), cvPoint(mask->width-1, mask->height-1),
41 cvScalarAll(1), 1, 8, 0 );
43 for( y = 0; y < img->height; y++ )
44 for( x = 0; x < img->width; x++ )
46 if( CV_IMAGE_ELEM(mask, uchar, y+1, x+1) != 0 )
48 cvFloodFill(img, cvPoint(x,y), cvScalarAll(cidx),
49 cvScalarAll(0), cvScalarAll(0), 0, 4, mask);
55 int main(int argc, char* argv[])
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;
68 for( i=0; i < 6; i++ )
70 int dx = (i%2)*250 - 30;
72 CvScalar white = cvRealScalar(255);
73 CvScalar black = cvRealScalar(0);
77 for( j = 0; j <= 10; j++ )
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);
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 );
100 cvNamedWindow( "image", 1 );
101 cvShowImage( "image", img );
102 cvConvert( img, img32f );
104 cvConvert( img32f, img32s );
106 cvFindContours( img32s, storage, &contours, sizeof(CvContour),
107 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
109 //cvFindContours( img, storage, &contours, sizeof(CvContour),
110 // CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
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);
119 // comment this out if you do not want approximation
120 contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
122 cvNamedWindow( "contours", 1 );
123 cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
126 CvRNG rng = cvRNG(-1);
128 CvSeq* tcontours = contours;
129 cvCvtColor( img, img3, CV_GRAY2BGR );
130 while( tcontours->h_next )
131 tcontours = tcontours->h_next;
133 for( ; tcontours != 0; tcontours = tcontours->h_prev )
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 )
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));
153 cvShowImage( "colored", img3 );
156 cvReleaseMemStorage( &storage );
157 cvReleaseImage( &img );
158 cvReleaseImage( &img32f );
159 cvReleaseImage( &img32s );
160 cvReleaseImage( &img3 );