3 printf("OpenCV Octave version of contours\n");
5 ## import the necessary things for OpenCV
15 ## some default constants
19 ## definition of some colors
20 _red = cvScalar (0, 0, 255, 0);
21 _green = cvScalar (0, 255, 0, 0);
22 _white = cvRealScalar (255);
23 _black = cvRealScalar (0);
25 ## the callback on the trackbar, to set the level of contours we want
27 function on_trackbar (position)
35 ## create the image for putting in it the founded contours
36 contours_image = cvCreateImage (cvSize (_SIZE, _SIZE), 8, 3);
38 ## compute the real level of display, given the current position
39 levels = position - 3;
45 ## zero or negative value
46 ## => get to the nearest face to make it look more funny
47 _contours = contours.h_next.h_next.h_next;
50 ## first, clear the image where we will draw contours
51 cvSetZero (contours_image);
53 ## draw contours in red and green
54 cvDrawContours (contours_image, _contours, _red, _green, levels, 3, cv.CV_AA, cvPoint (0, 0));
56 ## finally, show the image
57 cvShowImage ("contours", contours_image);
60 ## create the image where we want to display results
61 image = cvCreateImage (cvSize (_SIZE, _SIZE), 8, 1);
63 ## start with an empty image
66 ## draw the original picture
68 dx = mod(i,2) * 250 - 30;
72 cvPoint (dx + 150, dy + 100),
74 0, 0, 360, _white, -1, 8, 0);
76 cvPoint (dx + 115, dy + 70),
78 0, 0, 360, _black, -1, 8, 0);
80 cvPoint (dx + 185, dy + 70),
82 0, 0, 360, _black, -1, 8, 0);
84 cvPoint (dx + 115, dy + 70),
86 0, 0, 360, _white, -1, 8, 0);
88 cvPoint (dx + 185, dy + 70),
90 0, 0, 360, _white, -1, 8, 0);
92 cvPoint (dx + 115, dy + 70),
94 0, 0, 360, _black, -1, 8, 0);
96 cvPoint (dx + 185, dy + 70),
98 0, 0, 360, _black, -1, 8, 0);
100 cvPoint (dx + 150, dy + 100),
102 0, 0, 360, _black, -1, 8, 0);
104 cvPoint (dx + 150, dy + 150),
106 0, 0, 360, _black, -1, 8, 0);
108 cvPoint (dx + 27, dy + 100),
110 0, 0, 360, _white, -1, 8, 0);
112 cvPoint (dx + 273, dy + 100),
114 0, 0, 360, _white, -1, 8, 0);
117 ## create window and display the original picture in it
118 cvNamedWindow ("image", 1);
119 cvShowImage ("image", image);
121 ## create the storage area
122 storage = cvCreateMemStorage (0);
125 [nb_contours, contours] = cvFindContours (image, storage, sizeof_CvContour, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint (0,0));
127 ## comment this out if you do not want approximation
128 contours = cvApproxPoly (contours, sizeof_CvContour, storage, CV_POLY_APPROX_DP, 3, 1);
130 ## create the window for the contours
131 cvNamedWindow ("contours", 1);
133 ## create the trackbar, to enable the change of the displayed level
134 cvCreateTrackbar ("levels+3", "contours", 3, 7, @on_trackbar);
136 ## call one time the callback, so we will have the 1st display done
137 on_trackbar (_DEFAULT_LEVEL);
139 ## wait a key pressed to end