09-python opencv 绘制简单图形


  • 09-python opencv 绘制简单图形

    • 概述
    • 实现过程
      • 引用与创建空图
      • 绘制直线
      • 绘制矩形
      • 绘制圆
      • 绘制椭圆
      • 添加文字
      • 显示图像
    • 源代码
    • 运行结果
    • 参考

概述

本节实现的是使用OpenCV里自带的函数,绘制直线、长方形、圆形和椭圆。

  • 绘制直线
  • 绘制长方形
  • 绘制圆形
  • 绘制椭圆
  • 添加文字

实现过程

引用与创建空图

不再赘述,代码如下。

import cv2
import numpy# empty image
img = np.zeros((512, 512, 3), np.uint8)

绘制直线

使用opencv自带的line()函数绘制一条对角线,其声明如下:

cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])

其中各种参数的意义如下:
- img – Image.
- pt1 – First point of the line segment.
- pt2 – Second point of the line segment.
- color – Line color.
- thickness – Line thickness.
- lineType – Type of the line:

参数 意义
8 (or omitted) 8-connected line
4 4-connected line
CV_AA antialiased line

- shift – Number of fractional bits in the point coordinates.

在本文中,我绘制的直线从(0,0)到(511,511),线的颜色为蓝色(255,0,0),粗细为10,其他参数省略。

# draw a line
cv2.line(img, (0,0), (511,511), (255, 0, 0), 10)

绘制矩形

利用opencv自带的rectangle()函数可以绘制一个长方形,其声明如下:

cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) 

其中各种参数的意义如下:
- img – Image.
- pt1 – Vertex of the rectangle.
- pt2 – Vertex of the rectangle opposite to pt1 .
- rec – Alternative specification of the drawn rectangle.
- color – Rectangle color or brightness (grayscale image).
- thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
- lineType – Type of the line. See the line() description.
- shift – Number of fractional bits in the point coordinates.

本文中绘制一个长方形从(0,0)到(255,255),设置线的颜色为绿色(0,255,0),线的粗细为5,其他参数默认,如下:

# draw a rectangle
cv2.rectangle(img, (0,0), (255,255), (0, 255, 0), 5)

绘制圆

opencv里自带的circle()函数可以绘制以一个点为圆心特定半径的圆,其函数的声明如下:

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

其中各个参数的意义如下:
- 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.

本文中绘制的圆的圆心为(255,255),半径为127,线的颜色为红色,线的粗细为8,如下:

# draw a circle
cv2.circle(img, (255, 255), 127, (0, 0, 255), 8)

绘制椭圆

调用ellipse()函数绘制椭圆,其声明为:

cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) 

其中各个参数的意义如下:
- img – Image.
- center – Center of the ellipse.
- axes – Half of the size of the ellipse main axes.
- angle – Ellipse rotation angle in degrees.
- startAngle – Starting angle of the elliptic arc in degrees.
- endAngle – Ending angle of the elliptic arc in degrees.
- box – Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
- color – Ellipse color.
- thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
- lineType – Type of the ellipse boundary. See the line() description.
- shift – Number of fractional bits in the coordinates of the center and values of axes.

本文绘制了一个完整的椭圆,中心在(255,255),长轴150,短轴75,旋转0°,起始角度0°,终止角度360°,线的颜色为黄色(0,255,255),粗细为1。

# draw a ellipse
cv2.ellipse(img, (255, 255), (150, 75), 0, 0, 360, (0, 255, 255), 1) 

添加文字

opencv里的自带函数putText()可以在图像上加上文字,其声明如下:

cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

各个参数的意义如下:
- img – Image.
- text – Text string to be drawn.
- org – Bottom-left corner of the text string in the image.
- font – CvFont structure initialized using InitFont().
- fontFace – Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_ITALIC to get the slanted letters.
- fontScale – Font scale factor that is multiplied by the font-specific base size.
- color – Text color.
- thickness – Thickness of the lines used to draw a text.
- lineType – Line type. See the line for details.
- bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

本文在图像上显示OpenCV这几个字,如下:

font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2)

显示图像

显示最终绘制完的图像:

# show image
cv2.imshow("shape", img)cv2.waitKey(0)
cv2.destroyAllWindows()

源代码

整个程序的源代码如下:

# created by Huang Lu
# 27/08/2016 17:59:31
# Department of EE, Tsinghua Univ.import cv2
import numpy as np# empty image
img = np.zeros((512, 512, 3), np.uint8)# draw a line
cv2.line(img, (0,0), (511,511), (255, 0, 0), 10)# draw a rectangle
cv2.rectangle(img, (0,0), (255,255), (0, 255, 0), 5)# draw a circle
cv2.circle(img, (255, 255), 127, (0, 0, 255), 8)# draw a ellipse
cv2.ellipse(img, (255, 255), (150, 75), 0, 0, 360, (0, 255, 255), 1)# add text
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2)# show image
cv2.imshow("shape", img)cv2.waitKey(0)
cv2.destroyAllWindows()

也可以参考我的GitHub上的,点击这里。

运行结果

在命令行进入该源程序所在目录后,运行python main.py后即可显示结果。显示结果如下:

参考

  • http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
  • OpenCV-Python-Toturial-中文版.pdf
  • https://github.com/hlthu/Python/tree/master/Python_OpenCV/Draw_Shape/
  • https://hlthu.github.io/opencv/2016/06/12/python-opencv-a.html

python opencv 绘制简单图形相关推荐

  1. Python之Turtle库绘制简单图形

    Python之Turtle库绘制简单图形 来绘制正方形.三角形.五边形.五角形 import turtle #引入turtle库pen=turtle.Turtle() #创建turtle类型的画笔 # ...

  2. python的turtle怎么画曲线_利用 turtle库绘制简单图形

    turtle库是python的基础绘图库,这个库被介绍为一个最常用的用来介绍编程知识的方法库,其主要是用于程序设计入门,是标准库之一,利用turtle可以制作很多复杂的绘图. turtle名称含义为& ...

  3. Python使用turtle绘制简单图形-设置绝对坐标setpos(), 抬起画笔penup(),放下画笔pendown()

    [小白从小学Python.C.Java] [Python-计算机等级考试二级] [Python-数据分析] Python使用turtle绘制简单图形 [太阳]选择题 以下Python代码中的penup ...

  4. python matplotlib绘制函数图形_【总结篇】Python matplotlib之使用统计函数绘制简单图形...

    写在前面 作者注:我在这里只总结函数的功能及其用法,程序实例参考链接:link 我们用下面的语句来导入matplotlib库: 1import matplotlib.pyplot as plt 绘制简 ...

  5. python绘制3d图形-python matlibplot绘制3D图形

    本文实例为大家分享了python matlibplot绘制3D图形的具体代码,供大家参考,具体内容如下 1.散点图使用scatter from mpl_toolkits.mplot3d import ...

  6. [Qt教程] 第11篇 2D绘图(一)绘制简单图形

    [Qt教程] 第11篇 2D绘图(一)绘制简单图形 楼主  发表于 2013-4-23 12:52:35 | 查看: 1398| 回复: 5 绘制简单图形 版权声明 该文章原创于Qter开源社区,作者 ...

  7. java 绘制长方形_Java入门:绘制简单图形

    在上一节,我们学习了如何使用swing和awt工具创建一个空的窗口,本节学习如何绘制简单图形. 基本绘图介绍 Java中绘制基本图形,可以使用Java类库中的Graphics类,此类位于java.aw ...

  8. plotly绘制简单图形4--饼形图

    plotly绘制简单图形<1>--散点图折线图 plotly绘制简单图形<2>--条形图 plotly绘制简单图形<3>--设置按钮 本次说一下饼形图: 目录 1. ...

  9. plotly绘制简单图形5--饼形图附加

    plotly绘制简单图形<4>--饼形图里说了饼形图的基本设置, 这里补充一个怎样设置两个饼图的方法(数据是爬取京东手机和电脑价格数据分析结果) import plotly.plotly ...

最新文章

  1. 豪赌 ARM 梦碎:63 岁孙正义的「花甲历险记」
  2. C语言易错题集 第一部
  3. Phyton自定义包导入。
  4. linux 块编辑,vim中的可视块编辑
  5. java线程安全例子_Java总结篇系列:Java多线程(三)
  6. 中国移动试商用GPS手机导航业务 包月资费15元
  7. python线下培训班-线下培训价值一万八的某达PYTHON培训视频
  8. 【Alpha】“北航社团帮”小程序v1.0发布声明
  9. [design decision] user awareness: 自动安装还是不自动安装?
  10. hex2bin() 函数
  11. 删除了电脑硬盘的数据能恢复吗,硬盘数据删除了还能恢复吗
  12. 身份证号码中间显示*星号
  13. Isotropix Clarisse iFX Mac(CG渲染软件) v3.6破解版
  14. java 找不到符号
  15. 电脑网页出现服务器异常 请刷新页面重试,无法打开网页请尝试刷新页面是怎么回事?网页无法打开是什么原因...
  16. 解决xdp计算ip头checksum报错
  17. 【论文笔记】Disentangled Graph Collaborative Filtering --- SIGIR2020
  18. 基于ssm手机供应商管理系统
  19. 部署Python的框架下的web app的详细教程
  20. 打印html java 清晰度_java 利用jsp打印html页面

热门文章

  1. 精工机械表 调整时间,日期和星期的方法
  2. python实现提取视频里的语音转换为文字
  3. Word文档进行XXE攻击
  4. mysql表的基础操作: Create,,Retrieve,Update,Delete(大量示例)
  5. 网络隔离下的几种数据交换技术比较
  6. 2022年全球颈椎按摩仪市场前景分析及研究报告
  7. 记中国著名佛学书法家——释心仁
  8. 大学英语综合教程一 Unit 8 课文内容英译中 中英翻译
  9. 《好吗好的》--大冰
  10. 写给想学Linux系统的人