drawing.m 4.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#! /usr/bin/env octave

printf("OpenCV Octave version of drawing\n");

## import the necessary things for OpenCV
cv;
highgui;

function ret=random_color ()
  ret = CV_RGB(int32(rand()*255), int32(rand()*255), int32(rand()*255));
endfunction


## some "constants"
width = 1000;
height = 700;
window_name = "Drawing Demo";
number = 100;
delay = 5;
line_type = cv.CV_AA;  # change it to 8 to see non-antialiased graphics

## create the source image
image = cv.cvCreateImage (cv.cvSize (width, height), 8, 3);

## create window and display the original picture in it
highgui.cvNamedWindow (window_name, 1);
cv.cvSetZero (image);
highgui.cvShowImage (window_name, image);

## draw some lines
for i=0:number-1,
  pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
                    int32(rand() * 2 * height - height));
  pt2 = cv.cvPoint (int32(rand() * 2 * width - width),
                    int32(rand() * 2 * height - height));
  cv.cvLine (image, pt1, pt2,
             random_color (),
             int32(rand() * 10),
             line_type, 0);
  
  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## draw some rectangles
for i=0:number-1,
  pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
                    int32(rand() * 2 * height - height));
  pt2 = cv.cvPoint (int32(rand() * 2 * width - width),
                    int32(rand() * 2 * height - height));
  cv.cvRectangle (image, pt1, pt2,
                  random_color (),
                  int32(rand() * 10 - 1),
                  line_type, 0);
  
  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## draw some ellipes
for i=0:number-1,
  pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
                    int32(rand() * 2 * height - height));
  sz = cv.cvSize (int32(rand() * 200),
                  int32(rand() * 200));
  angle = rand() * 1000 * 0.180;
  cv.cvEllipse (image, pt1, sz, angle, angle - 100, angle + 200,
                random_color (),
                int32(rand() * 10 - 1),
                line_type, 0);
  
  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## init the list of polylines
nb_polylines = 2;
polylines_size = 3;
pt = cell(1, nb_polylines);
for a=1:nb_polylines,
  pt{a} = cell(1,polylines_size);
endfor

## draw some polylines
for i=0:number-1,
  for a=1:nb_polylines,
    for b=1:polylines_size,
      pt {a}{b} = cv.cvPoint (int32(rand() * 2 * width - width), \
                              int32(rand() * 2 * height - height));
    endfor
  endfor
  cv.cvPolyLine (image, pt, 1, random_color(), int32(rand() * 8 + 1), line_type, 0);

  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## draw some filled polylines
for i=0:number-1,
  for a=1:nb_polylines,
    for b=1:polylines_size,
      pt {a}{b} = cv.cvPoint (int32(rand() * 2 * width - width),
                              int32(rand() * 2 * height - height));
    endfor
  endfor
  cv.cvFillPoly (image, pt, random_color (), line_type, 0);

  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## draw some circles
for i=0:number-1,
  pt1 = cv.cvPoint (int32(rand() * 2 * width - width),
                    int32(rand() * 2 * height - height));
  cv.cvCircle (image, pt1, int32(rand() * 300), random_color (), \
	       int32(rand() * 10 - 1), line_type, 0);
  
  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## draw some text
for i=0:number-1,
  pt1 = cv.cvPoint (int32(rand() * 2 * width - width), \
                    int32(rand() * 2 * height - height));
  font = cv.cvInitFont (int32(rand() * 8), \
                        rand() * 100 * 0.05 + 0.01, \
                        rand() * 100 * 0.05 + 0.01, \
                        rand() * 5 * 0.1, \
                        int32(rand() * 10), \
                        line_type);

  cv.cvPutText (image, "Testing text rendering!", \
                pt1, font, \
                random_color ());
  
  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## prepare a text, and get it's properties
font = cv.cvInitFont (cv.CV_FONT_HERSHEY_COMPLEX, \
                      3, 3, 0.0, 5, line_type);
[text_size, ymin] = cv.cvGetTextSize ("OpenCV forever!", font);
pt1.x = int32((width - text_size.width) / 2);
pt1.y = int32((height + text_size.height) / 2);
image2 = cv.cvCloneImage(image);

## now, draw some OpenCV pub ;-)
for i=0:255-1,
  cv.cvSubS (image2, cv.cvScalarAll (i), image, []);
  cv.cvPutText (image, "OpenCV forever!",
                pt1, font, cv.cvScalar (255, i, i));
  highgui.cvShowImage (window_name, image);
  highgui.cvWaitKey (delay);
endfor

## wait some key to end
highgui.cvWaitKey (0);