Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / octave / drawing.m
1 #! /usr/bin/env octave
2
3 printf("OpenCV Octave version of drawing\n");
4
5 ## import the necessary things for OpenCV
6 cv;
7 highgui;
8
9 function ret=random_color ()
10   ret = CV_RGB(int32(rand()*255), int32(rand()*255), int32(rand()*255));
11 endfunction
12
13
14 ## some "constants"
15 width = 1000;
16 height = 700;
17 window_name = "Drawing Demo";
18 number = 100;
19 delay = 5;
20 line_type = cv.CV_AA;  # change it to 8 to see non-antialiased graphics
21
22 ## create the source image
23 image = cv.cvCreateImage (cv.cvSize (width, height), 8, 3);
24
25 ## create window and display the original picture in it
26 highgui.cvNamedWindow (window_name, 1);
27 cv.cvSetZero (image);
28 highgui.cvShowImage (window_name, image);
29
30 ## draw some lines
31 for i=0:number-1,
32   pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
33                     int32(rand() * 2 * height - height));
34   pt2 = cv.cvPoint (int32(rand() * 2 * width - width),
35                     int32(rand() * 2 * height - height));
36   cv.cvLine (image, pt1, pt2,
37              random_color (),
38              int32(rand() * 10),
39              line_type, 0);
40   
41   highgui.cvShowImage (window_name, image);
42   highgui.cvWaitKey (delay);
43 endfor
44
45 ## draw some rectangles
46 for i=0:number-1,
47   pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
48                     int32(rand() * 2 * height - height));
49   pt2 = cv.cvPoint (int32(rand() * 2 * width - width),
50                     int32(rand() * 2 * height - height));
51   cv.cvRectangle (image, pt1, pt2,
52                   random_color (),
53                   int32(rand() * 10 - 1),
54                   line_type, 0);
55   
56   highgui.cvShowImage (window_name, image);
57   highgui.cvWaitKey (delay);
58 endfor
59
60 ## draw some ellipes
61 for i=0:number-1,
62   pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
63                     int32(rand() * 2 * height - height));
64   sz = cv.cvSize (int32(rand() * 200),
65                   int32(rand() * 200));
66   angle = rand() * 1000 * 0.180;
67   cv.cvEllipse (image, pt1, sz, angle, angle - 100, angle + 200,
68                 random_color (),
69                 int32(rand() * 10 - 1),
70                 line_type, 0);
71   
72   highgui.cvShowImage (window_name, image);
73   highgui.cvWaitKey (delay);
74 endfor
75
76 ## init the list of polylines
77 nb_polylines = 2;
78 polylines_size = 3;
79 pt = cell(1, nb_polylines);
80 for a=1:nb_polylines,
81   pt{a} = cell(1,polylines_size);
82 endfor
83
84 ## draw some polylines
85 for i=0:number-1,
86   for a=1:nb_polylines,
87     for b=1:polylines_size,
88       pt {a}{b} = cv.cvPoint (int32(rand() * 2 * width - width), \
89                               int32(rand() * 2 * height - height));
90     endfor
91   endfor
92   cv.cvPolyLine (image, pt, 1, random_color(), int32(rand() * 8 + 1), line_type, 0);
93
94   highgui.cvShowImage (window_name, image);
95   highgui.cvWaitKey (delay);
96 endfor
97
98 ## draw some filled polylines
99 for i=0:number-1,
100   for a=1:nb_polylines,
101     for b=1:polylines_size,
102       pt {a}{b} = cv.cvPoint (int32(rand() * 2 * width - width),
103                               int32(rand() * 2 * height - height));
104     endfor
105   endfor
106   cv.cvFillPoly (image, pt, random_color (), line_type, 0);
107
108   highgui.cvShowImage (window_name, image);
109   highgui.cvWaitKey (delay);
110 endfor
111
112 ## draw some circles
113 for i=0:number-1,
114   pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
115                     int32(rand() * 2 * height - height));
116   cv.cvCircle (image, pt1, int32(rand() * 300), random_color (), \
117                int32(rand() * 10 - 1), line_type, 0);
118   
119   highgui.cvShowImage (window_name, image);
120   highgui.cvWaitKey (delay);
121 endfor
122
123 ## draw some text
124 for i=0:number-1,
125   pt1 = cv.cvPoint (int32(rand() * 2 * width - width), \
126                     int32(rand() * 2 * height - height));
127   font = cv.cvInitFont (int32(rand() * 8), \
128                         rand() * 100 * 0.05 + 0.01, \
129                         rand() * 100 * 0.05 + 0.01, \
130                         rand() * 5 * 0.1, \
131                         int32(rand() * 10), \
132                         line_type);
133
134   cv.cvPutText (image, "Testing text rendering!", \
135                 pt1, font, \
136                 random_color ());
137   
138   highgui.cvShowImage (window_name, image);
139   highgui.cvWaitKey (delay);
140 endfor
141
142 ## prepare a text, and get it's properties
143 font = cv.cvInitFont (cv.CV_FONT_HERSHEY_COMPLEX, \
144                       3, 3, 0.0, 5, line_type);
145 [text_size, ymin] = cv.cvGetTextSize ("OpenCV forever!", font);
146 pt1.x = int32((width - text_size.width) / 2);
147 pt1.y = int32((height + text_size.height) / 2);
148 image2 = cv.cvCloneImage(image);
149
150 ## now, draw some OpenCV pub ;-)
151 for i=0:255-1,
152   cv.cvSubS (image2, cv.cvScalarAll (i), image, []);
153   cv.cvPutText (image, "OpenCV forever!",
154                 pt1, font, cv.cvScalar (255, i, i));
155   highgui.cvShowImage (window_name, image);
156   highgui.cvWaitKey (delay);
157 endfor
158
159 ## wait some key to end
160 highgui.cvWaitKey (0);
161