OpenCV实践(5)- 基本绘图
来源:互联网 发布:sql2000数据库快速备份 编辑:程序博客网 时间:2024/06/04 18:48
1 目标
(1) 用Point绘制图像中的二维点;
(2) 使用Scalar,有什么用?
(3) 用OpenCV提供的line画线;
(4) 用OpenCV提供的ellipse画椭圆;
(5) 用OpenCV提供的rectangle画矩形;
(6) 用OpenCV提供的circle画圆;
(7) 用OpenCV提供的fillPoly画实心多边形;
2 说明
(1)Point
描述了一个2D点,指定了该像素点在图像中的坐标,(x,y)。我们可以如下定义:
Point pt;pt.x = 10;pt.y = 8;
或
Point pt = Point(10, 8);
(2)Scalar
表示具有4个元素的矢量vector。Scalar在OpenCV中被用来传递像素值。
在本文中我们用Scalar表述RGB色彩值。如果第四个元素不使用,可以不定义。
语法如下:
Scalar( a, b, c )
在这里,Red = c;Green = b;Blue = a。
3 代码实现
本文的代码可以在OpenCV源代码目录下的samples目录下找到。或者可以在这儿下载。
4 代码解释
(1)因为我们打算绘制两个图像-atom和rook,所以,我们需要绘制两个窗口进行显示:
/// Windows nameschar atom_window[] = "Drawing 1: Atom";char rook_window[] = "Drawing 2: Rook";/// Create black empty imagesMat atom_image = Mat::zeros( w, w, CV_8UC3 );Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
(2)我们创建几个不同的几何图形。对于该例,我们使用MyEllipse 和 MyFilledCircle绘制atom图像。
/// 1. Draw a simple atom:/// 1.a. Creating ellipsesMyEllipse( atom_image, 90 );MyEllipse( atom_image, 0 );MyEllipse( atom_image, 45 );MyEllipse( atom_image, -45 );/// 1.b. Creating circlesMyFilledCircle( atom_image, Point( w/2.0, w/2.0) );
(3)绘制rook对象
/// 2. Draw a rook/// 2.a. Create a convex polygonMyPolygon( rook_image );/// 2.b. Creating rectanglesrectangle( rook_image, Point( 0, 7*w/8.0 ), 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)让我们看一下使用的函数的内部实现。
a)MyLine
void MyLine( Mat img, Point start, Point end ){ int thickness = 2; int lineType = 8; line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType );}
如代码所示,就是调用了OpenCV的line函数。代码的意思不能理解,就不做解释了。
b)MyEllipse
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 ), /* a和b */ angle, /* 图形旋转角度 */ 0, 360, /* 0-360度延伸 */ Scalar( 255, 0, 0 ), /* 蓝色 */ thickness, /* 线粗 = 2 */ lineType );}
c)MyFilledCircle
void MyFilledCircle( Mat img, Point center ){ int thickness = -1; int lineType = 8; circle( img, center, w/32.0, /* 半径 */ Scalar( 0, 0, 255 ), /* 红色 */ thickness, /* = -1,将会被填充 */ lineType );}
d)MyPolygon
void MyPolygon( Mat img ){ int lineType = 8; /* 创建像素点 */ 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 ); }
e)rectangle
rectangle( rook_image, Point( 0, 7*w/8.0 ), Point( w, w), Scalar( 0, 255, 255 ), /* 黄色 */ -1, 8 );
5 运行结果
附代码
/** * @file Drawing_1.cpp * @brief Simple sample code */#include <opencv2/core/core.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>#define w 400#define FILLED -1 #define LINE_8 8using namespace cv;/// Function headersvoid 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 ){ //![create_images] /// 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 ); //![create_images] /// 1. Draw a simple atom: /// ----------------------- //![draw_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) ); //![draw_atom] /// 2. Draw a rook /// ------------------ //![draw_rook] /// 2.a. Create a convex polygon MyPolygon( rook_image ); //![rectangle] /// 2.b. Creating rectangles rectangle( rook_image, Point( 0, 7*w/8 ), Point( w, w), Scalar( 0, 255, 255 ), FILLED, LINE_8 ); //![rectangle] /// 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 ) ); //![draw_rook] /// 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//![myellipse]/** * @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 );}//![myellipse]//![myfilledcircle]/** * @function MyFilledCircle * @brief Draw a fixed-size filled circle */void MyFilledCircle( Mat img, Point center ){ circle( img, center, w/32, Scalar( 0, 0, 255 ), FILLED, LINE_8 );}//![myfilledcircle]//![mypolygon]/** * @function MyPolygon * @brief Draw a simple concave polygon (rook) */void MyPolygon( Mat img ){ int lineType = LINE_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 );}//![mypolygon]//![myline]/** * @function MyLine * @brief Draw a simple line */void MyLine( Mat img, Point start, Point end ){ int thickness = 2; int lineType = LINE_8; line( img, start, end, Scalar( 0, 0, 0 ), thickness, lineType );}//![myline]
0 0
- OpenCV实践(5)- 基本绘图
- opencv学习篇(7)基本绘图
- OpenCV之基本绘图
- OpenCV学习:基本绘图
- opencv的基本绘图
- openCV 绘图基本函数
- opencv-基本绘图函数
- OpenCV基本绘图
- OpenCV之基本绘图
- openCV—基本绘图
- Opencv基本绘图
- OpenCV之基本绘图
- opencv(c++)基本绘图
- OpenCv基本绘图操作
- OpenCV学习笔记(17)OpenCV之基本绘图
- OpenCV学习笔记(18)OpenCV之基本绘图2
- opencv 基本绘图函数总结
- 学习OpenCV范例(六)——基本绘图
- 怎样用JAVA打开一个已经写好的TXT文件? 是打开,不是读取啊!!!
- 明日操盘机器人选择题一(2017-02-02)
- Backtracking: 17. Letter Combinations of a Phone Number
- 2.2.3
- 明日操盘机器人选择题二(2017-02-02)
- OpenCV实践(5)- 基本绘图
- 明日操盘机器人选择题三(2017-02-02)
- 明日操盘机器人选择题四(2017-02-02)
- 移植7- uboot之sd卡做文文件系统启动linux
- PLSQL Trigger FOR EACH ROW
- [leetcode]--104. Maximum Depth of Binary Tree
- [leetcode]--389. Find the Difference
- 明日操盘机器人选择题五(2017-02-02)
- 新年感想