76d37e82a643fa0b989ee41a62eb62fc3fe89f48
[opencv] / samples / octave / contours.m
1 #! /usr/bin/env octave
2
3 printf("OpenCV Octave version of contours\n");
4
5 ## import the necessary things for OpenCV
6 cv;
7
8
9 global _SIZE;
10 global _red;
11 global _green;
12 global _blue;
13 global contours;
14
15 ## some default constants
16 _SIZE = 500;
17 _DEFAULT_LEVEL = 3;
18
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);
24
25 ## the callback on the trackbar, to set the level of contours we want
26 ## to display
27 function on_trackbar (position)
28   global cv;
29   global _SIZE;
30   global _red;
31   global _green;
32   global _blue;
33   global contours;
34
35   ## create the image for putting in it the founded contours
36   contours_image = cvCreateImage (cvSize (_SIZE, _SIZE), 8, 3);
37
38   ## compute the real level of display, given the current position
39   levels = position - 3;
40
41   ## initialisation
42   _contours = contours;
43   
44   if (levels <= 0)
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;
48   endif
49   
50   ## first, clear the image where we will draw contours
51   cvSetZero (contours_image);
52   
53   ## draw contours in red and green
54   cvDrawContours (contours_image, _contours, _red, _green, levels, 3, cv.CV_AA, cvPoint (0, 0));
55
56   ## finally, show the image
57   cvShowImage ("contours", contours_image);
58 endfunction
59
60 ## create the image where we want to display results
61 image = cvCreateImage (cvSize (_SIZE, _SIZE), 8, 1);
62
63 ## start with an empty image
64 cvSetZero (image);
65
66 ## draw the original picture
67 for i=0:6-1,
68   dx = mod(i,2) * 250 - 30;
69   dy = (i / 2) * 150;
70   
71   cvEllipse (image,
72                 cvPoint (dx + 150, dy + 100),
73                 cvSize (100, 70),
74                 0, 0, 360, _white, -1, 8, 0);
75   cvEllipse (image,
76                 cvPoint (dx + 115, dy + 70),
77                 cvSize (30, 20),
78                 0, 0, 360, _black, -1, 8, 0);
79   cvEllipse (image,
80                 cvPoint (dx + 185, dy + 70),
81                 cvSize (30, 20),
82                 0, 0, 360, _black, -1, 8, 0);
83   cvEllipse (image,
84                 cvPoint (dx + 115, dy + 70),
85                 cvSize (15, 15),
86                 0, 0, 360, _white, -1, 8, 0);
87   cvEllipse (image,
88                 cvPoint (dx + 185, dy + 70),
89                 cvSize (15, 15),
90                 0, 0, 360, _white, -1, 8, 0);
91   cvEllipse (image,
92                 cvPoint (dx + 115, dy + 70),
93                 cvSize (5, 5),
94                 0, 0, 360, _black, -1, 8, 0);
95   cvEllipse (image,
96                 cvPoint (dx + 185, dy + 70),
97                 cvSize (5, 5),
98                 0, 0, 360, _black, -1, 8, 0);
99   cvEllipse (image,
100                 cvPoint (dx + 150, dy + 100),
101                 cvSize (10, 5),
102                 0, 0, 360, _black, -1, 8, 0);
103   cvEllipse (image,
104                 cvPoint (dx + 150, dy + 150),
105                 cvSize (40, 10),
106                 0, 0, 360, _black, -1, 8, 0);
107   cvEllipse (image,
108                 cvPoint (dx + 27, dy + 100),
109                 cvSize (20, 35),
110                 0, 0, 360, _white, -1, 8, 0);
111   cvEllipse (image,
112                 cvPoint (dx + 273, dy + 100),
113                 cvSize (20, 35),
114                 0, 0, 360, _white, -1, 8, 0);
115 endfor
116
117 ## create window and display the original picture in it
118 cvNamedWindow ("image", 1);
119 cvShowImage ("image", image);
120
121 ## create the storage area
122 storage = cvCreateMemStorage (0);
123
124 ## find the contours
125 [nb_contours, contours] = cvFindContours (image, storage, sizeof_CvContour, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint (0,0));
126
127 ## comment this out if you do not want approximation
128 contours = cvApproxPoly (contours, sizeof_CvContour, storage, CV_POLY_APPROX_DP, 3, 1);
129
130 ## create the window for the contours
131 cvNamedWindow ("contours", 1);
132
133 ## create the trackbar, to enable the change of the displayed level
134 cvCreateTrackbar ("levels+3", "contours", 3, 7, @on_trackbar);
135
136 ## call one time the callback, so we will have the 1st display done
137 on_trackbar (_DEFAULT_LEVEL);
138
139 ## wait a key pressed to end
140 cvWaitKey (0);