首页 > 代码库 > OpenCV笔记(五)——基本的绘图操作

OpenCV笔记(五)——基本的绘图操作

用OpenCV的话,也要会一些绘图的操作。主要是画线、圆、矩形、椭圆。

 

绘图的话,首先要了解两种类型:Point和Scalar。

Point就是点的类,我们用它来表示图像当中的点,比如Point pt; pt.x = 10; pt.y = 8;或者Point pt = Point(10, 8);

Scalar实质上就是4维的向量,也就是C++当中的含有4个元素的vector。一般我们只用到三维,比如Scalar(0, 0, 255);

 

一、线

void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

pt1表示线段的起始点。

pt2表示线段的终止点。

color表示线段的颜色。

lineType有三种类型:8连接的线、4连接的线,经过高斯平滑的线。分别是8, 4, CV_AA。

shift暂时不清楚有什么用,默认为0就好。

 

二、圆

void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

center表示圆心坐标。

radius表示半径。

thickness为圆的曲线的厚度,如果thickness为负数,则圆被填满。

lineType的意思和line()函数当中lineType的意思一样。

 

三、矩形

void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, lineType=8, int shift=0)

pt1和pt2是对角线上的两个点

void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, lineType=8, int shift=0)

Rect是矩形类型,一般以x, y, width, height初始化,比如 Rect rect(10, 10, 200, 100),表示从点(10,10)开始,宽200高100的矩形

 

四、椭圆

void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

要知道axes的意思,首先要知道Size。Size是尺寸类型,一般以width, height初始化,Size size(50, 20)表示宽度为50,高度为20。axes是axis的复数形式,Size的第一个参数表示长半轴的长度,第二个参数表示短半轴的长度。

angle是椭圆相对图像的角度。

startAngle和endAngle是椭圆的圆弧的角度,0到360,说明是整个椭圆。0到180,只有半个椭圆。OpenCV采取逆时针绘制。

void ellipse(Mat& img, RotatedRect& box, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

box给出一个旋转的矩形,然后ellipse方法给出在矩形的内接椭圆。RotatedRect(const Point2f& center, const Size2f& size, float angle)

用box给椭圆初始化和用axes+angle的差别在于:box中RotatedRect的Size是矩形的宽度和高度,而axes是椭圆的半轴的长度。

 

附OpenCV官方指南当中绘图操作的实例代码:

  1 /**  2  * @file Drawing_1.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( void ){ 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, w/2) ); 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( w, w), 54          Point( 0, 7*w/8 ), 55           56          Scalar( 0, 255, 255 ), 57          -1, 58          8 ); 59     */ 60   Rect rec(0, 7*w/8, w, 1*w/8); 61   rectangle(rook_image, rec, Scalar(0, 255, 255), -1, 8); 62   /// 2.c. Create a few lines 63   MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) ); 64   MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) ); 65   MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) ); 66   MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) ); 67  68   /// 3. Display your stuff! 69   imshow( atom_window, atom_image ); 70   moveWindow( atom_window, 0, 200 ); 71   imshow( rook_window, rook_image ); 72   moveWindow( rook_window, w, 200 ); 73  74   waitKey( 0 ); 75   return(0); 76 } 77  78 /// Function Declaration 79  80 /** 81  * @function MyEllipse 82  * @brief Draw a fixed-size ellipse with different angles 83  */ 84 void MyEllipse( Mat img, double angle ) 85 { 86   int thickness = 2; 87   int lineType = 8; 88    89   ellipse( img, 90        //Point( w/2, w/2 ), 91        //Size( w/4, w/16 ), 92        //angle, 93        //0, 94        //360, 95        RotatedRect(Point( w/2, w/2 ),Size( w/2, w/8 ),angle), 96        Scalar( 255, 0, 0 ), 97        thickness, 98        lineType ); 99      /*  100   ellipse( img,101       Point( w/2, w/2 ),102       Size( w/4, w/16 ),103       angle,104       0,105       360,106       Scalar( 255, 0, 0 ),107       thickness,108       lineType );109       */110 }111 112 /**113  * @function MyFilledCircle114  * @brief Draw a fixed-size filled circle115  */116 void MyFilledCircle( Mat img, Point center )117 {118   int thickness = -1;119   int lineType = CV_AA;120 121   circle( img,122       center,123       w/32,124       Scalar( 0, 0, 255 ),125       thickness,126       lineType,127       0);128 }129 130 /**131  * @function MyPolygon132  * @function Draw a simple concave polygon (rook)133  */134 void MyPolygon( Mat img )135 {136   int lineType = 8;137 138   /** Create some points */139   Point rook_points[1][20];140   rook_points[0][0]  = Point(    w/4,   7*w/8 );141   rook_points[0][1]  = Point(  3*w/4,   7*w/8 );142   rook_points[0][2]  = Point(  3*w/4,  13*w/16 );143   rook_points[0][3]  = Point( 11*w/16, 13*w/16 );144   rook_points[0][4]  = Point( 19*w/32,  3*w/8 );145   rook_points[0][5]  = Point(  3*w/4,   3*w/8 );146   rook_points[0][6]  = Point(  3*w/4,     w/8 );147   rook_points[0][7]  = Point( 26*w/40,    w/8 );148   rook_points[0][8]  = Point( 26*w/40,    w/4 );149   rook_points[0][9]  = Point( 22*w/40,    w/4 );150   rook_points[0][10] = Point( 22*w/40,    w/8 );151   rook_points[0][11] = Point( 18*w/40,    w/8 );152   rook_points[0][12] = Point( 18*w/40,    w/4 );153   rook_points[0][13] = Point( 14*w/40,    w/4 );154   rook_points[0][14] = Point( 14*w/40,    w/8 );155   rook_points[0][15] = Point(    w/4,     w/8 );156   rook_points[0][16] = Point(    w/4,   3*w/8 );157   rook_points[0][17] = Point( 13*w/32,  3*w/8 );158   rook_points[0][18] = Point(  5*w/16, 13*w/16 );159   rook_points[0][19] = Point(    w/4,  13*w/16 );160 161   const Point* ppt[1] = { rook_points[0] };162   int npt[] = { 20 };163 164   fillPoly( img,165         ppt,166         npt,167             1,168         Scalar( 255, 255, 255 ),169         lineType );170 }171 172 /**173  * @function MyLine174  * @brief Draw a simple line175  */176 void MyLine( Mat img, Point start, Point end )177 {178   int thickness = 2;179   int lineType = 8;180   line( img,181     start,182     end,183     Scalar( 0, 0, 0 ),184     thickness,185     lineType,186     0);187 }
View Code

 

OpenCV笔记(五)——基本的绘图操作