参考:
1、http://docs.opencv.org/3.3.0/ 官方文档api
2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程
3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程
5、https://docs.opencv.org/3.3.0/index.html 官方英文教程
6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials
7、https://www.learnopencv.com/
8、http://answers.opencv.org/questions/ OpenCV论坛
注:安装的版本 opencv_python-3.3.0-cp36-cp36m-win_amd64.whl
参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html
提示:opencv 颜色默认采样BGR格式
OpenCV中的绘图功能
目标
- Learn to draw different geometric shapes with OpenCV
- You will learn these functions : cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse(),cv2.putText() etc.
代码
- img : 你想要画出形状的图像
- color : 形状的颜色. for BGR, pass it as a tuple, eg:
(255,0,0)for blue. For grayscale, just pass the scalar value. - thickness : 厚度 Thickness of the line or circle etc. 对于封闭的形状,如圆,-1 将填充这个形状 ,默认厚度为 1
- lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected.
cv2.LINE_AAgives anti-aliased line which looks great for curves.
画线
画线,只需给点线的起始坐标。 We will create a black image and draw a blue line on it from top-left to bottom-right corners.import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a diagonal blue line with thickness of 5 px img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
画矩形
画矩形,需要矩形的上-左角和下-右角坐标。在图像的上-右角画一个绿色的矩形
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a diagonal blue line with thickness of 5 px # img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
画圆
画圆,需要给点圆心和半径。
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a diagonal blue line with thickness of 5 px # img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) img = cv2.circle(img,(447,63), 63, (0,0,255), -1) # -1 表示填充 cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
画椭圆
画椭圆,中心位置(x,y),轴长(长半轴,短半轴),旋转角(顺时针旋转),起始角度,终止角度(按顺时针方向画),
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a diagonal blue line with thickness of 5 px # img = cv2.line(img,(0,0),(511,511),(0,0,255),5,cv2.LINE_AA) # opencv里默认是 BGR格式 ;(255,0,0) 代表 Blue img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) # img = cv2.circle(img,(447,63), 63, (0,0,255), -1) # -1 表示填充 img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1) # 这里的255 相当于(255,0,0) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
画多边形
先要给定各个顶点的坐标
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Here we draw a small polygon of with four vertices in yellow color. pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) pts = pts.reshape((-1,1,2)) img = cv2.polylines(img,[pts],True,(0,255,255),3) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
cv2.polylines() can be used to draw multiple lines. 将第三个参数设置成FALSE
在图像上写文本:
To put texts in images, you need specify following things.- 想要写的文本
- 放置的位置 (i.e. bottom-left corner where data starts).
- 字体类型 (Check cv2.putText() docs for supported fonts)
- 字体的比例(大小) (specifies the size of font)
- regular things like color, thickness, lineType etc. For better look,
lineType = cv2.LINE_AAis recommended.
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) font = cv2.FONT_HERSHEY_SIMPLEX # 字体 cv2.putText(img,'OpenCV',(10,256), font, 4,(255,255,255),2,cv2.LINE_AA) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
Exercises
- Try to create the logo of OpenCV using drawing functions available in OpenCV
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) img = cv2.ellipse(img,(128,256),(50,50),0,0,270,(0,255,0),15) img = cv2.ellipse(img,(256,256),(50,50),-90,0,270,(255,0,0),15) img = cv2.ellipse(img,(191,166),(50,50),135,0,270,(0,0,255),15) font = cv2.FONT_HERSHEY_SIMPLEX # 字体 cv2.putText(img,'OpenCV',(10,386), font, 4,(255,255,255),2,cv2.LINE_AA) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows()
本文介绍如何使用OpenCV绘制不同几何形状,包括线、矩形、圆、椭圆及多边形等,并演示如何在图像中添加文本。通过具体实例代码展示了各函数的用法及参数设置。
27万+

被折叠的 条评论
为什么被折叠?



