有时程序中需要画一些基础的图形,例如直线,矩形,椭圆以及多边形。OpenCV中当然有此类函数。

1.函数介绍

直线line:

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 (or omitted) - 8-connected 线
4 - 4-connected 线.
CV_AA - 抗锯齿线.
shift – 分位的点坐标

椭圆ellipse:

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)
void ellipse(Mat& img, const RotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)
参数说明:
img – 图像
center – 椭圆中心
axes – 椭圆主半轴长度
angle –旋转角度
startAngle – 椭圆弧起始角度
endAngle – 椭圆弧终止角度
box – Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
color – 颜色
thickness – 粗细,如果小于0,表示填充椭圆
lineType – 和line函数一样,直线类型
shift – 部分点位坐标

矩形rectangle:

void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
参数说明:
img – 图像
pt1 – 顶点坐标
pt2 – 与p1相对的顶点坐标
rec – 矩形的选择规范
color – 矩形的颜色或亮度
thickness – 和椭圆函数一样
lineType – 和line函数一样
shift – 部分点位坐标

圆circle:

void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
参数说明:
img – Image where the circle is drawn.
center – Center of the circle.
radius – Radius of the circle.
color – Circle color.
thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
lineType – Type of the circle boundary. See the line() description.
shift – Number of fractional bits in the coordinates of the center and in the radius value.

多边形fillPoly:

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 – Image.
pts – Array of polygons where each polygon is represented as an array of points.
npts – Array of polygon vertex counters.
ncontours – Number of contours that bind the filled region.
color – Polygon color.
lineType – Type of the polygon boundaries. See the line() description.
shift – Number of fractional bits in the vertex coordinates.
offset – Optional offset of all points of the contours.

2.代码

#define w 400

/// 函数定义
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end ); int BasicDraw( void ){ char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook"; Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 ); MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 ); MyFilledCircle( atom_image, Point( w/2, w/2) ); MyPolygon( rook_image ); rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
-1,
8 ); 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 ) ); imshow( atom_window, atom_image );
moveWindow( atom_window, 0, 200 );
imshow( rook_window, rook_image );
moveWindow( rook_window, w, 200 ); waitKey( 0 );
return(0);
} //画椭圆的函数
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 );
} //画圆
void MyFilledCircle( Mat img, Point center )
{
int thickness = -1;
int lineType = 8; circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
thickness,
lineType );
} //画多边形
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 );
} //画直线的函数
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = 8;
line( img,
start,
end,
Scalar( 0, 0, 0 ),
thickness,
lineType );
}

3.结果

4.其他说明

Point结构:

定义一个”点“,x参数和y参数。

Scalar结构:

Scalar是有四个元素的容器,可以只使用其部分元素,例如上面使用Scalar(a,b,c)来表示颜色的RGB。

5.结束

OpenCV入门:(六:基础画图函数)的更多相关文章

  1. 数据分析与展示——Matplotlib基础绘图函数示例

    Matplotlib库入门 Matplotlib基础绘图函数示例 pyplot基础图表函数概述 函数 说明 plt.plot(x,y,fmt, ...) 绘制一个坐标图 plt.boxplot(dat ...

  2. C#基础入门 六

    C#基础入门 六 静态类进阶 静态构造方法 用于初始化任何静态数据,或用于执行仅需执行一次的特定操作,在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数,静态构造方法是无参数的. publ ...

  3. [易学易懂系列|rustlang语言|零基础|快速入门|(7)|函数Functions与闭包Closure]

    [易学易懂系列|rustlang语言|零基础|快速入门|(7)函数Functions与闭包Closure] 有意思的基础知识 函数Functions与闭包Closure 我们今天再来看看函数. 在Ru ...

  4. [OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑

    http://blog.csdn.net/poem_qianmo/article/details/25560901 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog ...

  5. 【OpenCV入门教程之十四】OpenCV霍夫变换:霍夫线变换,霍夫圆变换合辑

    http://blog.csdn.net/poem_qianmo/article/details/26977557 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog ...

  6. 【OpenCV入门指南】第一篇 安装OpenCV

    http://blog.csdn.net/morewindows/article/details/8225783/ win10下vs2015配置Opencv3.1.0过程详解(转) http://ww ...

  7. 【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析(转)

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/20537737 作者:毛星云(浅墨)  ...

  8. OpenCV入门学习笔记

    OpenCV入门学习笔记 参照OpenCV中文论坛相关文档(http://www.opencv.org.cn/) 一.简介 OpenCV(Open Source Computer Vision),开源 ...

  9. opencv ,亮度调整【【OpenCV入门教程之六】 创建Trackbar & 图像对比度、亮度值调整

    http://blog.csdn.net/poem_qianmo/article/details/21479533 [OpenCV入门教程之六] 创建Trackbar & 图像对比度.亮度值调 ...

随机推荐

  1. 【转】Android Fragment 真正的完全解析(上)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...

  2. C# 利用HttpWebRequest进行HTTPS的post请求的示例

    最近一个推送信息的目标接口从http格式换成https格式,原来的请求无法正常发送,所以修改了发送请求的方法.标红的代码是新加了,改了之后就可以正常访问(不检测证书的) public static s ...

  3. while、dowhile、switchcase 循环嵌套、穷举、迭代

    for(var i=0;i<5;i++)//假如规定到5 { document.write("123456<br/>") } 如果变成这样 var i=0: fo ...

  4. CSharp调用C++编写的DLL的方法

    自己比较懒,有的时候想写点东西,但由于文笔不行.技术不行也就没有怎么写.经常是用到什么.学习什么的时候,简单写点,权当是个学习笔记.上博客的次数也很少,有人给我留言也是没有怎么及时的回复,深感抱歉! ...

  5. Entity Framework 一

    本篇主要介绍:EntityFramework简介, 实体框架架构图, EF版本 实体框架: 编写和管理数据访问的ADO.Net代码是一件单调乏味的工作.微软已经提供了一个名为“实体框架”的O / RM ...

  6. 使用两个嵌套的for循坏探测2-100的所有素数

    只能被1和本身整除的整数才叫做素数 public class prime { public static void main(String[] args) { ; i <= ; i++) { ; ...

  7. Python基础—15-正则表达式

    正则表达式 应用场景 特定规律字符串的查找替换切割等 邮箱格式.URL.IP等的校验 爬虫项目中,特定内容的提取 使用原则 只要是能够使用字符串函数解决的问题,就不要使用正则 正则的效率较低,还会降低 ...

  8. Oracle单行函数用法

    单行函数分为五种类型:字符函数.数值函数.日期函数.转换函数.通用函数. 1.字符函数: 对于输入的字符转换为需要转为的字符或数值. upper()大写 --小写字母转为大写字母 --对于表指定的字符 ...

  9. nmon监控linux系统性能

    Nmon是一款计算机性能系统监控工具,使用 Nmon 可以很轻松的监控系统的 CPU.内存.网络.硬盘.文件系统.NFS.高耗进程.资源等信息.[简单方便] Nmon 安装 到https://sour ...

  10. Configuration 中无法自动注入依赖于component的bean

    出现问题时我这样使用依赖注入 @Configuration public class WebServiceConfig { @Autowired private IMessageWebService ...