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 MyFilledCircle
114  * @brief Draw a fixed-size filled circle
115  */
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 MyPolygon
132  * @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 MyLine
174  * @brief Draw a simple line
175  */
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 }

时间: 2024-08-02 15:12:40

OpenCV笔记(五)——基本的绘图操作的相关文章

Python学习笔记五:字符串常用操作,字典,三级菜单实例

字符串常用操作 7月19日,7月20日 ,7月22日,7月29日,8月29日,2月29日 首字母大写:a_str.capitalize() 统计字符串个数:a_str.count("x") 输出字符,不够的使用指定的字符补上,字符居中:a_str.center(50,"-") 判断字符串以什么结尾:a_str.endwith("xx") 将字符串中的tab转换为指定数目的空格:a_str.expandtabs(tabsize=30) 查找指定字符

Caliburn.Micro学习笔记(五)----协同IResult

Caliburn.Micro学习笔记(五)----协同IResult 今天说一下协同IResult 看一下IResult接口 /// <summary> /// Allows custom code to execute after the return of a action. /// </summary> public interface IResult { /// <summary> /// Executes the result using the specif

【OpenCV笔记】使用VS2012和OpenCV2.4.9搭建配置OpenCV开发环境

使用MS Visual C++来创建OpenCV工程,由于不同的VS版本在配置时有所差别,现特把配置过程总结下来,以方便自己和其他朋友使用. 1.软件准备 安装Visual Studio2012和OpenCV2.4.9 这里就不再对软件的安装和环境变量的设置进行说明了,其他类似的文章的设置都大抵相似,本文重点介绍一下VS2012环境的配置. 2.配置VS2012环境 (1)新建工程 你可以创建简单的控制台应用或者拥有图形用户界面的复杂应用,此处我们选择最简单常用的控制台应用. 理解解决方案(So

OpenCV笔记(九)——更多的形态学变换

Erode和Dilate是基本的形态学运算,根据这两种运算,我们能够组成更多形态学运算. 一.开运算 Openning dst = open(src, element) = dilate(erode(src, element)) 开运算就是将一幅图像先腐蚀再膨胀,主要作用是移除白色的小区域. 二.闭运算 Closing dst = close(src, element) = erode(dilate(src, element)) 闭运算就是将一幅图像先膨胀再腐蚀,主要作用是移除黑色的小区域. 三

OpenCV for Python 学习 (一 绘图函数)

本人的学习笔记主要记录的是学习opencv-python-tutorials这本书中的笔记 今天晚上简单学习OpenCV for Python如何绘图,主要用了这几个函数(这几个函数可在:http://docs.opencv.org/modules/core/doc/drawing_functions.html 找到): cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) cv2.circle(img, center,

NLTK学习笔记(五):分类和标注词汇

[TOC] 词性标注器 之后的很多工作都需要标注完的词汇.nltk自带英文标注器pos_tag import nltk text = nltk.word_tokenize("And now for something compleyely difference") print(text) print(nltk.pos_tag(text)) 标注语料库 表示已经标注的标识符:nltk.tag.str2tuple('word/类型') text = "The/AT grand/J

Linux System Programming 学习笔记(五) 进程管理

1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity inside of a process the virtualization of memory is associated with the process, the threads all share the same memory address space 2. pid The idle pro

MySQL学习笔记之五 有关数据表操作

MySQL在创建表的时候,创建一个.frm文件保存表和列定义.索引存储在一个有.MYI(MYindex)扩展名的文件并且数据存储在有.MYD(MYData)扩展名的文件中.   一.用SHOW/ DESCRIBE语句显示数据表的信息 语法: SHOW TABLES [FROM db_name] [LIKE wild] or SHOW COLUMNS FROM tbl_name [FROM db_name] [LIKE wild] or SHOW INDEX FROM tbl_name [FROM

[JAVA_开课吧资源]第五周 I/O操作、多线程、网络编程技术

主题一 I/O操作 » 流的概念 在面向对象语言中, 数据的输入和输出都是通过数据流来实现的.数据流是一组有顺序.有起点和终点的字符集合.就好比是两个不同的池子,一个池子中存满了水,而另一个池子中则没有任何的东西,在这两个水池中安放一个管子,水就可以从一个池子流向另一个池子了.在从一个池子向另一个池子输送水的过程中,水扮演的角色就是数据流. [请点击查看更多内容 转自文章] » Stream stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源.在Java的IO中,所有