Home | History | Annotate | Download | only in Matrix
      1 /**
      2  * @file Drawing_2.cpp
      3  * @brief Simple sample code
      4  */
      5 
      6 #include <opencv2/core.hpp>
      7 #include <opencv2/imgproc.hpp>
      8 #include <opencv2/highgui.hpp>
      9 #include <iostream>
     10 #include <stdio.h>
     11 
     12 using namespace cv;
     13 
     14 /// Global Variables
     15 const int NUMBER = 100;
     16 const int DELAY = 5;
     17 
     18 const int window_width = 900;
     19 const int window_height = 600;
     20 int x_1 = -window_width/2;
     21 int x_2 = window_width*3/2;
     22 int y_1 = -window_width/2;
     23 int y_2 = window_width*3/2;
     24 
     25 /// Function headers
     26 static Scalar randomColor( RNG& rng );
     27 int Drawing_Random_Lines( Mat image, char* window_name, RNG rng );
     28 int Drawing_Random_Rectangles( Mat image, char* window_name, RNG rng );
     29 int Drawing_Random_Ellipses( Mat image, char* window_name, RNG rng );
     30 int Drawing_Random_Polylines( Mat image, char* window_name, RNG rng );
     31 int Drawing_Random_Filled_Polygons( Mat image, char* window_name, RNG rng );
     32 int Drawing_Random_Circles( Mat image, char* window_name, RNG rng );
     33 int Displaying_Random_Text( Mat image, char* window_name, RNG rng );
     34 int Displaying_Big_End( Mat image, char* window_name, RNG rng );
     35 
     36 
     37 /**
     38  * @function main
     39  */
     40 int main( void )
     41 {
     42   int c;
     43 
     44   /// Start creating a window
     45   char window_name[] = "Drawing_2 Tutorial";
     46 
     47   /// Also create a random object (RNG)
     48   RNG rng( 0xFFFFFFFF );
     49 
     50   /// Initialize a matrix filled with zeros
     51   Mat image = Mat::zeros( window_height, window_width, CV_8UC3 );
     52   /// Show it in a window during DELAY ms
     53   imshow( window_name, image );
     54   waitKey( DELAY );
     55 
     56   /// Now, let's draw some lines
     57   c = Drawing_Random_Lines(image, window_name, rng);
     58   if( c != 0 ) return 0;
     59 
     60   /// Go on drawing, this time nice rectangles
     61   c = Drawing_Random_Rectangles(image, window_name, rng);
     62   if( c != 0 ) return 0;
     63 
     64   /// Draw some ellipses
     65   c = Drawing_Random_Ellipses( image, window_name, rng );
     66   if( c != 0 ) return 0;
     67 
     68   /// Now some polylines
     69   c = Drawing_Random_Polylines( image, window_name, rng );
     70   if( c != 0 ) return 0;
     71 
     72   /// Draw filled polygons
     73   c = Drawing_Random_Filled_Polygons( image, window_name, rng );
     74   if( c != 0 ) return 0;
     75 
     76   /// Draw circles
     77   c = Drawing_Random_Circles( image, window_name, rng );
     78   if( c != 0 ) return 0;
     79 
     80   /// Display text in random positions
     81   c = Displaying_Random_Text( image, window_name, rng );
     82   if( c != 0 ) return 0;
     83 
     84   /// Displaying the big end!
     85   c = Displaying_Big_End( image, window_name, rng );
     86   if( c != 0 ) return 0;
     87 
     88   waitKey(0);
     89   return 0;
     90 }
     91 
     92 /// Function definitions
     93 
     94 /**
     95  * @function randomColor
     96  * @brief Produces a random color given a random object
     97  */
     98 static Scalar randomColor( RNG& rng )
     99 {
    100   int icolor = (unsigned) rng;
    101   return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
    102 }
    103 
    104 
    105 /**
    106  * @function Drawing_Random_Lines
    107  */
    108 int Drawing_Random_Lines( Mat image, char* window_name, RNG rng )
    109 {
    110   Point pt1, pt2;
    111 
    112   for( int i = 0; i < NUMBER; i++ )
    113   {
    114     pt1.x = rng.uniform( x_1, x_2 );
    115     pt1.y = rng.uniform( y_1, y_2 );
    116     pt2.x = rng.uniform( x_1, x_2 );
    117     pt2.y = rng.uniform( y_1, y_2 );
    118 
    119     line( image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8 );
    120     imshow( window_name, image );
    121     if( waitKey( DELAY ) >= 0 )
    122       { return -1; }
    123   }
    124 
    125   return 0;
    126 }
    127 
    128 /**
    129  * @function Drawing_Rectangles
    130  */
    131 int Drawing_Random_Rectangles( Mat image, char* window_name, RNG rng )
    132 {
    133   Point pt1, pt2;
    134   int lineType = 8;
    135   int thickness = rng.uniform( -3, 10 );
    136 
    137   for( int i = 0; i < NUMBER; i++ )
    138   {
    139     pt1.x = rng.uniform( x_1, x_2 );
    140     pt1.y = rng.uniform( y_1, y_2 );
    141     pt2.x = rng.uniform( x_1, x_2 );
    142     pt2.y = rng.uniform( y_1, y_2 );
    143 
    144     rectangle( image, pt1, pt2, randomColor(rng), MAX( thickness, -1 ), lineType );
    145 
    146     imshow( window_name, image );
    147     if( waitKey( DELAY ) >= 0 )
    148       { return -1; }
    149   }
    150 
    151   return 0;
    152 }
    153 
    154 /**
    155  * @function Drawing_Random_Ellipses
    156  */
    157 int Drawing_Random_Ellipses( Mat image, char* window_name, RNG rng )
    158 {
    159   int lineType = 8;
    160 
    161   for ( int i = 0; i < NUMBER; i++ )
    162   {
    163     Point center;
    164     center.x = rng.uniform(x_1, x_2);
    165     center.y = rng.uniform(y_1, y_2);
    166 
    167     Size axes;
    168     axes.width = rng.uniform(0, 200);
    169     axes.height = rng.uniform(0, 200);
    170 
    171     double angle = rng.uniform(0, 180);
    172 
    173     ellipse( image, center, axes, angle, angle - 100, angle + 200,
    174              randomColor(rng), rng.uniform(-1,9), lineType );
    175 
    176     imshow( window_name, image );
    177 
    178     if( waitKey(DELAY) >= 0 )
    179       { return -1; }
    180   }
    181 
    182   return 0;
    183 }
    184 
    185 /**
    186  * @function Drawing_Random_Polylines
    187  */
    188 int Drawing_Random_Polylines( Mat image, char* window_name, RNG rng )
    189 {
    190   int lineType = 8;
    191 
    192   for( int i = 0; i< NUMBER; i++ )
    193   {
    194     Point pt[2][3];
    195     pt[0][0].x = rng.uniform(x_1, x_2);
    196     pt[0][0].y = rng.uniform(y_1, y_2);
    197     pt[0][1].x = rng.uniform(x_1, x_2);
    198     pt[0][1].y = rng.uniform(y_1, y_2);
    199     pt[0][2].x = rng.uniform(x_1, x_2);
    200     pt[0][2].y = rng.uniform(y_1, y_2);
    201     pt[1][0].x = rng.uniform(x_1, x_2);
    202     pt[1][0].y = rng.uniform(y_1, y_2);
    203     pt[1][1].x = rng.uniform(x_1, x_2);
    204     pt[1][1].y = rng.uniform(y_1, y_2);
    205     pt[1][2].x = rng.uniform(x_1, x_2);
    206     pt[1][2].y = rng.uniform(y_1, y_2);
    207 
    208     const Point* ppt[2] = {pt[0], pt[1]};
    209     int npt[] = {3, 3};
    210 
    211     polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);
    212 
    213     imshow( window_name, image );
    214     if( waitKey(DELAY) >= 0 )
    215       { return -1; }
    216   }
    217   return 0;
    218 }
    219 
    220 /**
    221  * @function Drawing_Random_Filled_Polygons
    222  */
    223 int Drawing_Random_Filled_Polygons( Mat image, char* window_name, RNG rng )
    224 {
    225   int lineType = 8;
    226 
    227   for ( int i = 0; i < NUMBER; i++ )
    228   {
    229     Point pt[2][3];
    230     pt[0][0].x = rng.uniform(x_1, x_2);
    231     pt[0][0].y = rng.uniform(y_1, y_2);
    232     pt[0][1].x = rng.uniform(x_1, x_2);
    233     pt[0][1].y = rng.uniform(y_1, y_2);
    234     pt[0][2].x = rng.uniform(x_1, x_2);
    235     pt[0][2].y = rng.uniform(y_1, y_2);
    236     pt[1][0].x = rng.uniform(x_1, x_2);
    237     pt[1][0].y = rng.uniform(y_1, y_2);
    238     pt[1][1].x = rng.uniform(x_1, x_2);
    239     pt[1][1].y = rng.uniform(y_1, y_2);
    240     pt[1][2].x = rng.uniform(x_1, x_2);
    241     pt[1][2].y = rng.uniform(y_1, y_2);
    242 
    243     const Point* ppt[2] = {pt[0], pt[1]};
    244     int npt[] = {3, 3};
    245 
    246     fillPoly( image, ppt, npt, 2, randomColor(rng), lineType );
    247 
    248     imshow( window_name, image );
    249     if( waitKey(DELAY) >= 0 )
    250        { return -1; }
    251   }
    252   return 0;
    253 }
    254 
    255 /**
    256  * @function Drawing_Random_Circles
    257  */
    258 int Drawing_Random_Circles( Mat image, char* window_name, RNG rng )
    259 {
    260   int lineType = 8;
    261 
    262   for (int i = 0; i < NUMBER; i++)
    263   {
    264     Point center;
    265     center.x = rng.uniform(x_1, x_2);
    266     center.y = rng.uniform(y_1, y_2);
    267 
    268     circle( image, center, rng.uniform(0, 300), randomColor(rng),
    269             rng.uniform(-1, 9), lineType );
    270 
    271     imshow( window_name, image );
    272     if( waitKey(DELAY) >= 0 )
    273       { return -1; }
    274   }
    275 
    276   return 0;
    277 }
    278 
    279 /**
    280  * @function Displaying_Random_Text
    281  */
    282 int Displaying_Random_Text( Mat image, char* window_name, RNG rng )
    283 {
    284   int lineType = 8;
    285 
    286   for ( int i = 1; i < NUMBER; i++ )
    287   {
    288     Point org;
    289     org.x = rng.uniform(x_1, x_2);
    290     org.y = rng.uniform(y_1, y_2);
    291 
    292     putText( image, "Testing text rendering", org, rng.uniform(0,8),
    293              rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
    294 
    295     imshow( window_name, image );
    296     if( waitKey(DELAY) >= 0 )
    297       { return -1; }
    298   }
    299 
    300   return 0;
    301 }
    302 
    303 /**
    304  * @function Displaying_Big_End
    305  */
    306 int Displaying_Big_End( Mat image, char* window_name, RNG )
    307 {
    308   Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
    309   Point org((window_width - textsize.width)/2, (window_height - textsize.height)/2);
    310   int lineType = 8;
    311 
    312   Mat image2;
    313 
    314   for( int i = 0; i < 255; i += 2 )
    315   {
    316     image2 = image - Scalar::all(i);
    317     putText( image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
    318              Scalar(i, i, 255), 5, lineType );
    319 
    320     imshow( window_name, image2 );
    321     if( waitKey(DELAY) >= 0 )
    322        { return -1; }
    323   }
    324 
    325   return 0;
    326 }
    327