1 #include <opencv2/imgproc/imgproc_c.h>
2 #include <opencv2/highgui/highgui.hpp>
6 printf("\nThis program creates an image to demonstrate the use of the \"c\" contour\n"
7 "functions: cvFindContours() and cvApproxPoly() along with the storage\n"
8 "functions cvCreateMemStorage() and cvDrawContours().\n"
9 "It also shows the use of a trackbar to control contour retrieval.\n"
19 void on_trackbar(int pos)
21 IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
22 CvSeq* _contours = contours;
23 int _levels = levels - 3;
24 if( _levels <= 0 ) // get to the nearest face to make it look more funny
25 _contours = _contours->h_next->h_next->h_next;
27 cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
28 cvShowImage( "contours", cnt_img );
29 cvReleaseImage( &cnt_img );
34 int main( int argc, char** argv )
37 CvMemStorage* storage = cvCreateMemStorage(0);
38 IplImage* img = cvCreateImage( cvSize(w,w), 8, 1 );
42 for( i=0; i < 6; i++ )
44 int dx = (i%2)*250 - 30;
46 CvScalar white = cvRealScalar(255);
47 CvScalar black = cvRealScalar(0);
51 for( j = 0; j <= 10; j++ )
53 double angle = (j+5)*CV_PI/21;
54 cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),
55 cvRound(dy+100-90*sin(angle))),
56 cvPoint(cvRound(dx+100+j*10-30*cos(angle)),
57 cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
61 cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
62 cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
63 cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
64 cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
65 cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
66 cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
67 cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
68 cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
69 cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
70 cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
71 cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
74 cvNamedWindow( "image", 1 );
75 cvShowImage( "image", img );
77 cvFindContours( img, storage, &contours, sizeof(CvContour),
78 CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
81 const char* attrs[] = {"recursive", "1", 0};
82 cvSave("contours.xml", contours, 0, 0, cvAttrList(attrs, 0));
83 contours = (CvSeq*)cvLoad("contours.xml", storage, 0, 0);
86 // comment this out if you do not want approximation
87 contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
89 cvNamedWindow( "contours", 1 );
90 cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
94 cvReleaseMemStorage( &storage );
95 cvReleaseImage( &img );