opencv-基本绘图函数

来源:互联网 发布:如何登陆我的淘宝店铺 编辑:程序博客网 时间:2024/04/30 15:57

基本绘图相对来说也是非常简单的,只是几个函数的调用而已,在这里OpenCV教程中已经讲得非常详细了,我这里只是贴出代码,运行结果,函数参数解析和函数功能。

1、绘图

  • 用OpenCV的函数 line 绘 直线
  • 用OpenCV的函数 ellipse 绘 椭圆
  • 用OpenCV的函数 rectangle 绘 矩形
  • 用OpenCV的函数 circle 绘 圆
  • 用OpenCV的函数 fillPoly 绘 填充的多边形

2、代码实现

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2.   
  3.   
  4. /** 
  5.  * @file Drawing_1.cpp 
  6.  * @brief Simple sample code 
  7.  */  
  8.   
  9. #include <opencv2/core/core.hpp>  
  10. #include <opencv2/highgui/highgui.hpp>  
  11.   
  12. #define w 400  
  13.   
  14. using namespace cv;  
  15.   
  16. /// Function headers  
  17. void MyEllipse( Mat img, double angle );  
  18. void MyFilledCircle( Mat img, Point center );  
  19. void MyPolygon( Mat img );  
  20. void MyLine( Mat img, Point start, Point end );  
  21.   
  22. /** 
  23.  * @function main 
  24.  * @brief Main function 
  25.  */  
  26. int main( void ){  
  27.   
  28.     /// 窗口名字  
  29.     char atom_window[] = "Drawing 1: Atom";  
  30.     char rook_window[] = "Drawing 2: Rook";  
  31.   
  32.     /// 创建空全黑像素的空图像  
  33.     Mat atom_image = Mat::zeros( w, w, CV_8UC3 );  
  34.     Mat rook_image = Mat::zeros( w, w, CV_8UC3 );  
  35.   
  36.     /// 1. 画一个简单的原子。  
  37.   
  38.     /// 1.a. 创建椭圆  
  39.     MyEllipse( atom_image, 90 );  
  40.     MyEllipse( atom_image, 0 );  
  41.     MyEllipse( atom_image, 45 );  
  42.     MyEllipse( atom_image, -45 );  
  43.   
  44.     /// 1.b. 创建圆  
  45.     MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );  
  46.   
  47.     /// 2. 画一个赌棍  
  48.   
  49.     /// 2.a. 创建一个凸多边形  
  50.     MyPolygon( rook_image );  
  51.   
  52.     /// 2.b. 创建矩形  
  53.     rectangle( rook_image,  
  54.         Point( 0, 7*w/8.0 ),  
  55.         Point( w, w),  
  56.         Scalar( 0, 255, 255 ),  
  57.         -1,  
  58.         8 );  
  59.   
  60.     /// 2.c. 画几条直线  
  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.显示图片,并且将窗口移动到指定的位置  
  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.   /** 建立一些点*/  
  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. }  

3、运行结果

 
                             图1、原子    
  
                                             图2、赌棍

4、用到的类和函数

Point:

功能:数据结构表示了由其图像坐标 x 和 y 指定的2D点
定义:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Point pt;  
  2. pt.x = 10;  
  3. pt.y = 8;  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Point pt =  Point(10, 8);  

Scalar:

功能:具有4个元素的数组,可以用它来表示RGB颜色值
参数表达式:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Scalar( a, b, c )  
那么定义的RGB颜色值为: Red = cGreen = b and Blue = a

line:

功能:画一条直线连接两个点
结构:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)  
img:输入图片
pt1:起始点
pt2:终点
color:划线的颜色
thickness:线的粗细程度
lineType:线的类型,默认为8联通 

ellipse:

功能:画椭圆、弧线、扇形
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 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)  
img:输入图片
center:椭圆中心
axes:椭圆轴的长度
angle:椭圆旋转角度
startAngle:椭圆弧度开始的角度
endAngle:椭圆弧度结束的角度
color:颜色值
thickness:线的粗细程度
lineType:线的类型,默认为8联通
原理图:
../../../_images/ellipse.png

circle:

功能:画一个圆
结构:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)  
img:输入图片
center:圆点坐标
radius:圆的半径
color:颜色值
thickness:线的粗细程度,如果为负数,则说明圆内被填充。
lineType:线的类型,默认为8联通

fillPoly:

功能:填充多边形
结构:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )  
img:输入图片
pts:多边形顶点集
npts:要绘制的多边形顶点数目
ncontours:要绘制的多边形数目
color:颜色值
thickness:线的粗细程度,如果为负数,则说明圆内被填充。
lineType:线的类型,默认为8联通

rectangle:

功能:画矩形
结构:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)  
img:输入图片
pt1、pt2:两个对角顶点
color:颜色值
thickness:线的粗细程度,如果为负数,则说明圆内被填充。
lineType:线的类型,默认为8联通


注:本人发现在main函数中没有用到nameWindow()函数,直接使用inshow()函数,并且也能显示出窗口和图片,那么为什么要多一个nameWindow()这样的函数呢,是否多此一举,希望高手指点。




0 0
原创粉丝点击