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