OpenCV基本画图功能

来源:互联网 发布:找数据软件 编辑:程序博客网 时间:2024/06/14 16:21
[cpp] view plaincopy
  1. /** 
  2.  * @file Drawing.cpp 
  3.  * @brief Simple sample code 
  4.  */  
  5.   
  6. #include <opencv2/core/core.hpp>  
  7. #include <opencv2/highgui/highgui.hpp>  
  8.   
  9. #define w 400  
  10.   
  11. using namespace cv;  
  12.   
  13. /// Function headers  
  14. void MyEllipse( Mat img, double angle );  
  15. void MyFilledCircle( Mat img, Point center );  
  16. void MyPolygon( Mat img );  
  17. void MyLine( Mat img, Point start, Point end );  
  18.   
  19. /** 
  20.  * @function main 
  21.  * @brief Main function 
  22.  */  
  23. int main( int argc, char **argv ){  
  24.   
  25.   /// Windows names  
  26.   char atom_window[] = "Drawing 1: Atom";  
  27.   char rook_window[] = "Drawing 2: Rook";  
  28.   
  29.   /// Create black empty images   
  30.   Mat atom_image = Mat::zeros( w, w, CV_8UC3 );  
  31.   Mat rook_image = Mat::zeros( w, w, CV_8UC3 );  
  32.   
  33.   /// 1. Draw a simple atom:  
  34.   /// -----------------------  
  35.   
  36.   /// 1.a. Creating ellipses   
  37.   MyEllipse( atom_image, 90 );  
  38.   MyEllipse( atom_image, 0 );  
  39.   MyEllipse( atom_image, 45 );  
  40.   MyEllipse( atom_image, -45 );  
  41.   
  42.   /// 1.b. Creating circles  
  43.   MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );  
  44.   
  45.   /// 2. Draw a rook  
  46.   /// ------------------  
  47.   
  48.   /// 2.a. Create a convex polygon  
  49.   MyPolygon( rook_image );  
  50.   
  51.   /// 2.b. Creating rectangles  
  52.   rectangle( rook_image,  
  53.          Point( 0, 7*w/8.0 ),  
  54.          Point( w, w),  
  55.          Scalar( 0, 255, 255 ),  
  56.          -1,  
  57.          8 );  
  58.   
  59.   /// 2.c. Create a few lines   
  60.   MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );  
  61.   MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );  
  62.   MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );  
  63.   MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );  
  64.   
  65.   /// 3. Display your stuff!  
  66.   imshow( atom_window, atom_image );  
  67.   cvMoveWindow( atom_window, 0, 200 );  
  68.   imshow( rook_window, rook_image );  
  69.   cvMoveWindow( rook_window, w, 200 );  
  70.   
  71.   waitKey( 0 );  
  72.   return(0);  
  73. }  
  74.   
  75. /// Function Declaration  
  76.   
  77. /** 
  78.  * @function MyEllipse 
  79.  * @brief Draw a fixed-size ellipse with different angles 
  80.  */  
  81. void MyEllipse( Mat img, double angle )  
  82. {  
  83.   int thickness = 2;  
  84.   int lineType = 8;  
  85.   
  86.   ellipse( img,  
  87.        Point( w/2.0, w/2.0 ),  
  88.        Size( w/4.0, w/16.0 ),  
  89.        angle,  
  90.        0,   
  91.        360,  
  92.        Scalar( 255, 0, 0 ),  
  93.        thickness,  
  94.        lineType );    
  95. }  
  96.   
  97. /** 
  98.  * @function MyFilledCircle 
  99.  * @brief Draw a fixed-size filled circle 
  100.  */  
  101. void MyFilledCircle( Mat img, Point center )  
  102. {  
  103.   int thickness = -1;  
  104.   int lineType = 8;  
  105.   
  106.   circle( img,   
  107.       center,  
  108.       w/32.0,  
  109.       Scalar( 0, 0, 255 ),  
  110.       thickness,   
  111.       lineType );  
  112. }  
  113.   
  114. /** 
  115.  * @function MyPolygon 
  116.  * @function Draw a simple concave polygon (rook) 
  117.  */  
  118. void MyPolygon( Mat img )  
  119. {  
  120.   int lineType = 8;  
  121.   
  122.   /** Create some points */  
  123.   Point rook_points[1][20];  
  124.   rook_points[0][0] = Point( w/4.0, 7*w/8.0 );  
  125.   rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );  
  126.   rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );  
  127.   rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );  
  128.   rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );  
  129.   rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );  
  130.   rook_points[0][6] = Point( 3*w/4.0, w/8.0 );  
  131.   rook_points[0][7] = Point( 26*w/40.0, w/8.0 );  
  132.   rook_points[0][8] = Point( 26*w/40.0, w/4.0 );  
  133.   rook_points[0][9] = Point( 22*w/40.0, w/4.0 );  
  134.   rook_points[0][10] = Point( 22*w/40.0, w/8.0 );  
  135.   rook_points[0][11] = Point( 18*w/40.0, w/8.0 );  
  136.   rook_points[0][12] = Point( 18*w/40.0, w/4.0 );  
  137.   rook_points[0][13] = Point( 14*w/40.0, w/4.0 );  
  138.   rook_points[0][14] = Point( 14*w/40.0, w/8.0 );  
  139.   rook_points[0][15] = Point( w/4.0, w/8.0 );  
  140.   rook_points[0][16] = Point( w/4.0, 3*w/8.0 );  
  141.   rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );  
  142.   rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );  
  143.   rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;  
  144.   
  145.   const Point* ppt[1] = { rook_points[0] };  
  146.   int npt[] = { 20 };  
  147.   
  148.   fillPoly( img,  
  149.         ppt,  
  150.         npt,  
  151.             1,  
  152.         Scalar( 255, 255, 255 ),  
  153.         lineType );           
  154. }  
  155.   
  156. /** 
  157.  * @function MyLine 
  158.  * @brief Draw a simple line 
  159.  */  
  160. void MyLine( Mat img, Point start, Point end )  
  161. {  
  162.   int thickness = 2;  
  163.   int lineType = 8;  
  164.   line( img,   
  165.     start,  
  166.     end,  
  167.     Scalar( 0, 0, 0 ),  
  168.     thickness,  
  169.     lineType );  
  170. }  



0 0