基本的图形绘制
2016-03-03 09:28
267 查看
目标:
在该教程中你将学会如何去:
使用Point在图像中定义一个二维点;
使用Scalar以及为什么它有用;
使用OpenCV函数line绘制一条直线;
使用ellipse函数绘制一个椭圆
使用rectangle函数绘制矩形;
使用circle函数绘制圆形;
使用fillPoly绘制一个填充的多边形;
OpenCV理论:
在这个教程中,我们将着重使用两个结构:Point和Scalar。
Point:
它表示一个二维点,用图像的x和y坐标来表示。定义如下:
Pointpt;
pt.x = 10;
pt.y = 8;
或者:
Point pt=Point(10,8);
Scalar:
表示一个有4个元素的vector容器。Scalar在OpenCV中通常用来传递像素值。
在这个教程中,我们将把它扩展到去表示BGR颜色值(3个参数),最后一个参数如果没有用它就没必要去定义它。
让我们来看一个例子,如果我们要求一个颜色参数,那么我们给出下面的代码:
Scalar(a,b,c)
我们可以定义一个BGR颜色,例如:Blue=a,Green=b,Red=c
代码:
解释:
1、因为我们打算去绘制两幅图像(一个atom和一个rook),我们需要去创建2个图像以及两个窗口去显示他们。
2、然后我们创建函数去绘制不同的几何形状;
3、绘制rook的时候我们调用MyLine,rectangle和MyPolygon
Point( w, w), Scalar( 0, 255, 255 ), -1, 8 );/// 2.c. Create a few linesMyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );MyLine(
rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
4、让我们来看看这些函数里面是什么:
正如我们所看到的,MyLine函数仅仅调用了函数line:
从起点start到终点end之间绘制一条直线;
这条线在图像img中显示;
线的颜色被Scalar定义为黑色;
线宽被设置为2;
线的类型属于8连通的。
椭圆在img中显示;
椭圆中心在点(w/2,w/2)并且被一个大小为(w/4,w/16)的矩形所包络;
这个椭圆旋转了angle角度;
这个椭圆外延了0-360°之间的弧度;
图像的颜色被定义为Scalar(255,0,0),在BGR中表示Blue蓝色;
这个椭圆的线宽为2;
图像在Img中显示;
圆形的圆心在center;
圆的半径是w/32;
圆的颜色定义为Scalar(0,0,225),在BGR中表示红色;
Thickness=-1,表示圆形将被填充。
多边形在img中显示;
多边形的定点被设置为ppt中的点;
顶点的总数在npt中;
多边形的数量是1,即只绘制一个多边形;
颜色被设置为白色。
图像在rook_image中显示;
对角线上的顶点坐标被Point(0,7*w/8)和Point(w,w)定义;
颜色设置为黄色;
Thickness=-1,表示图像将被填充
效果:
在该教程中你将学会如何去:
使用Point在图像中定义一个二维点;
使用Scalar以及为什么它有用;
使用OpenCV函数line绘制一条直线;
使用ellipse函数绘制一个椭圆
使用rectangle函数绘制矩形;
使用circle函数绘制圆形;
使用fillPoly绘制一个填充的多边形;
OpenCV理论:
在这个教程中,我们将着重使用两个结构:Point和Scalar。
Point:
它表示一个二维点,用图像的x和y坐标来表示。定义如下:
Pointpt;
pt.x = 10;
pt.y = 8;
或者:
Point pt=Point(10,8);
Scalar:
表示一个有4个元素的vector容器。Scalar在OpenCV中通常用来传递像素值。
在这个教程中,我们将把它扩展到去表示BGR颜色值(3个参数),最后一个参数如果没有用它就没必要去定义它。
让我们来看一个例子,如果我们要求一个颜色参数,那么我们给出下面的代码:
Scalar(a,b,c)
我们可以定义一个BGR颜色,例如:Blue=a,Green=b,Red=c
代码:
#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #define w 400 using namespace cv; /// Function headers void MyEllipse( Mat img, double angle ); void MyFilledCircle( Mat img, Point center ); void MyPolygon( Mat img ); void MyLine( Mat img, Point start, Point end ); /** * @function main * @brief Main function */ int main( void ){ /// Windows names char atom_window[] = "Drawing 1: Atom"; char rook_window[] = "Drawing 2: Rook"; /// Create black empty images Mat atom_image = Mat::zeros( w, w, CV_8UC3 ); Mat rook_image = Mat::zeros( w, w, CV_8UC3 ); /// 1. Draw a simple atom: /// ----------------------- /// 1.a. Creating ellipses MyEllipse( atom_image, 90 ); MyEllipse( atom_image, 0 ); MyEllipse( atom_image, 45 ); MyEllipse( atom_image, -45 ); /// 1.b. Creating circles MyFilledCircle( atom_image, Point( w/2, w/2) ); /// 2. Draw a rook /// ------------------ /// 2.a. Create a convex polygon MyPolygon( rook_image ); /// 2.b. Creating rectangles rectangle( rook_image, Point( 0, 7*w/8 ), Point( w, w), Scalar( 0, 255, 255 ), -1, 8 ); /// 2.c. Create a few lines MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) ); MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) ); MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) ); MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) ); /// 3. Display your stuff! imshow( atom_window, atom_image ); moveWindow( atom_window, 0, 200 ); imshow( rook_window, rook_image ); moveWindow( rook_window, w, 200 ); waitKey( 0 ); return(0); } /// Function Declaration /** * @function MyEllipse * @brief Draw a fixed-size ellipse with different angles */ void MyEllipse( Mat img, double angle ) { int thickness = 2; int lineType = 8; ellipse( img, Point( w/2, w/2 ), Size( w/4, w/16 ), angle, 0, 360, Scalar( 255, 0, 0 ), thickness, lineType ); } /** * @function MyFilledCircle * @brief Draw a fixed-size filled circle */ void MyFilledCircle( Mat img, Point center ) { int thickness = -1; int lineType = 8; circle( img, center, w/32, Scalar( 0, 0, 255 ), thickness, lineType ); } /** * @function MyPolygon * @function Draw a simple concave polygon (rook) */ void MyPolygon( Mat img ) { int lineType = 8; /** Create some points */ Point rook_points[1][20]; rook_points[0][0] = Point( w/4, 7*w/8 ); rook_points[0][1] = Point( 3*w/4, 7*w/8 ); rook_points[0][2] = Point( 3*w/4, 13*w/16 ); rook_points[0][3] = Point( 11*w/16, 13*w/16 ); rook_points[0][4] = Point( 19*w/32, 3*w/8 ); rook_points[0][5] = Point( 3*w/4, 3*w/8 ); rook_points[0][6] = Point( 3*w/4, w/8 ); rook_points[0][7] = Point( 26*w/40, w/8 ); rook_points[0][8] = Point( 26*w/40, w/4 ); rook_points[0][9] = Point( 22*w/40, w/4 ); rook_points[0][10] = Point( 22*w/40, w/8 ); rook_points[0][11] = Point( 18*w/40, w/8 ); rook_points[0][12] = Point( 18*w/40, w/4 ); rook_points[0][13] = Point( 14*w/40, w/4 ); rook_points[0][14] = Point( 14*w/40, w/8 ); rook_points[0][15] = Point( w/4, w/8 ); rook_points[0][16] = Point( w/4, 3*w/8 ); rook_points[0][17] = Point( 13*w/32, 3*w/8 ); rook_points[0][18] = Point( 5*w/16, 13*w/16 ); rook_points[0][19] = Point( w/4, 13*w/16 ); const Point* ppt[1] = { rook_points[0] }; int npt[] = { 20 }; fillPoly( img, ppt, npt, 1, Scalar( 255, 255, 255 ), lineType ); } /** * @function MyLine * @brief Draw a simple line */ void MyLine( Mat img, Point start, Point end ) { int thickness = 2; int lineType = 8; line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType ); }
解释:
1、因为我们打算去绘制两幅图像(一个atom和一个rook),我们需要去创建2个图像以及两个窗口去显示他们。
/// Windows names char atom_window[] = "Drawing 1: Atom"; char rook_window[] = "Drawing 2: Rook"; /// Create black empty images Mat atom_image = Mat::zeros( w, w, CV_8UC3 ); Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
2、然后我们创建函数去绘制不同的几何形状;
MyEllipse( atom_image, 90 ); MyEllipse( atom_image, 0 ); MyEllipse( atom_image, 45 ); MyEllipse( atom_image, -45 ); MyFilledCircle( atom_image, Point( w/2.0, w/2.0) );
3、绘制rook的时候我们调用MyLine,rectangle和MyPolygon
MyPolygon( rook_image ); /// 2.b. Creating rectangles rectangle( rook_image, Point( 0, 7*w/8.0 ),<pre name="code" class="cpp">void MyLine( Mat img, Point start, Point end ) { int thickness = 2; int lineType = 8; line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType ); }
Point( w, w), Scalar( 0, 255, 255 ), -1, 8 );/// 2.c. Create a few linesMyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );MyLine(
rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
4、让我们来看看这些函数里面是什么:
void MyLine( Mat img, Point start, Point end ) { int thickness = 2; int lineType = 8; line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType ); }
正如我们所看到的,MyLine函数仅仅调用了函数line:
从起点start到终点end之间绘制一条直线;
这条线在图像img中显示;
线的颜色被Scalar定义为黑色;
线宽被设置为2;
线的类型属于8连通的。
void MyEllipse( Mat img, double angle ) { int thickness = 2; int lineType = 8; ellipse( img, Point( w/2.0, w/2.0 ), Size( w/4.0, w/16.0 ), angle, 0, 360, Scalar( 255, 0, 0 ), thickness, lineType ); }
椭圆在img中显示;
椭圆中心在点(w/2,w/2)并且被一个大小为(w/4,w/16)的矩形所包络;
这个椭圆旋转了angle角度;
这个椭圆外延了0-360°之间的弧度;
图像的颜色被定义为Scalar(255,0,0),在BGR中表示Blue蓝色;
这个椭圆的线宽为2;
void MyFilledCircle( Mat img, Point center ) { int thickness = -1; int lineType = 8; circle( img, center, w/32.0, Scalar( 0, 0, 255 ), thickness, lineType ); }
图像在Img中显示;
圆形的圆心在center;
圆的半径是w/32;
圆的颜色定义为Scalar(0,0,225),在BGR中表示红色;
Thickness=-1,表示圆形将被填充。
void MyPolygon( Mat img ) { int lineType = 8; /** Create some points */ Point rook_points[1][20]; rook_points[0][0] = Point( w/4.0, 7*w/8.0 ); rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 ); rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 ); rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 ); rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 ); rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 ); rook_points[0][6] = Point( 3*w/4.0, w/8.0 ); rook_points[0][7] = Point( 26*w/40.0, w/8.0 ); rook_points[0][8] = Point( 26*w/40.0, w/4.0 ); rook_points[0][9] = Point( 22*w/40.0, w/4.0 ); rook_points[0][10] = Point( 22*w/40.0, w/8.0 ); rook_points[0][11] = Point( 18*w/40.0, w/8.0 ); rook_points[0][12] = Point( 18*w/40.0, w/4.0 ); rook_points[0][13] = Point( 14*w/40.0, w/4.0 ); rook_points[0][14] = Point( 14*w/40.0, w/8.0 ); rook_points[0][15] = Point( w/4.0, w/8.0 ); rook_points[0][16] = Point( w/4.0, 3*w/8.0 ); rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 ); rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 ); rook_points[0][19] = Point( w/4.0, 13*w/16.0) ; const Point* ppt[1] = { rook_points[0] }; int npt[] = { 20 }; fillPoly( img, ppt, npt, 1, Scalar( 255, 255, 255 ), lineType ); }
多边形在img中显示;
多边形的定点被设置为ppt中的点;
顶点的总数在npt中;
多边形的数量是1,即只绘制一个多边形;
颜色被设置为白色。
rectangle( rook_image, Point( 0, 7*w/8.0 ), Point( w, w), Scalar( 0, 255, 255 ), -1, 8 );
图像在rook_image中显示;
对角线上的顶点坐标被Point(0,7*w/8)和Point(w,w)定义;
颜色设置为黄色;
Thickness=-1,表示图像将被填充
效果:
相关文章推荐
- 解决图片朝向旋转的问题
- Netron源码解读(一):GraphControl画布对象
- [Selenium] 怎样判断是否适合自动化测试
- C#实现listview Group收缩扩展的方法
- 正则表达式解析xml文档
- 国内某电商大佬面试题
- spring boot
- 选择排序
- Leetcode ☞ 142. Linked List Cycle II ☆
- Android ABD input(Keyevent) 参数
- srs之ST
- 多行文本溢出显示省略号(…)全攻略
- forward 和redirect
- lintcode-easy-Partition List
- Hadoop Pig学习笔记 各种SQL在PIG中实现
- 第三方知乎专栏应用Android源码
- LogiScope100MHZ 16通道逻辑分析仪
- 深入了解java虚拟机
- awk
- 解决新浪微盘下载没反应下载中断