文章目录
关于本期blog,一个值得注意的细节:
在前边文章中修改个别像素值的时候,我们可以对像素数组组直接取某个像素时,如
image[300, 200] = [255, 255, 255]
即 把以左上角为画布坐标原点,竖直方向上第301个,水平方向上第201个像素修改为白色。
但是在本篇中,为了更加快捷高效地操作像素,我们将使用OpenCV提供的一系列操作像素的方法,在以下方法中,通常需要传入坐标点。这里传入的坐标点,与上边手动操作像素时的坐标点有所不同,这里通常是传入一个数组 如(a, b),来表示坐标。依然是以左上角为画布的坐标原点,但是a的位置是水平方向,b的位置是数值方向。这一点与手动操作是相反的。学习过程中要注意到这个细节。
1. cv2.line()绘制线段
语法
cv2.line()
line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
- img 即表示画布(图像)的数组
- pt1指起始点的坐标,元组格式
- pt2指结束点的坐标,元组格式
- color 表示颜色的元组,如在BGR色彩空间中,(255, 0, 0)表示蓝色,(0, 255, 0)表示绿色,(0, 0, 255)表示红色。
- thickness 线条宽度
- lineType 线条产生的算法类型,可以是4或者8,默认为8。两种算法绘制出的线条视觉上看不出差别。
以绘制四条不同的颜色、粗细程度,并依次首尾相连的线段。
import numpy as np import cv2 # 创建画布(黑色画布)(三维数组) canvas = np.zeros((300, 300, 3), np.uint8) # canvas = np.ones((300, 300, 3), np.uint8) * 255 # 如果创建白色画布 canvas = cv2.line(canvas, (50, 50), (50, 250), (255, 0, 0), 5) canvas = cv2.line(canvas, (50, 250), (250, 250), (0, 255, 0), 10) canvas = cv2.line(canvas, (250, 250), (250, 50), (0, 0, 255), 15) canvas = cv2.line(canvas, (250, 50), (50, 50), (0, 255, 255), 20) cv2.imshow("Lines", canvas) cv2.waitKey() cv2.destroyAllWindows()
程序执行结果如图所示:
2. cv2.rectangle() 绘制矩形
语法
cv2.rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
pt1表示矩形左上角坐标,pt2表示矩形右下角坐标。其他参数语法同上。
thickness如果为负责表示所绘图形为实心图形,内部采用颜色填充。
以白色为画布底色为例。
import numpy as np import cv2 canvas = np.ones((500, 600, 3), np.uint8) * 255 canvas = cv2.rectangle(canvas, (150, 200), (400, 350), (255, 255, 0), 20) cv2.imshow("Rectangle", canvas) cv2.waitKey() cv2.destroyAllWindows()
3. cv2.circle() 绘制圆形
circle(img, center, radius, color, thickness=None, lineType=None, shift=None)
- center 为圆心坐标
- radius为半径长度
3.1 绘制实现圆 与 空心圆
import numpy as np # 导入Python中的numpy模块 import cv2 canvas = np.ones((500, 500, 3), np.uint8) * 255 # 绘制三个实心圆和一个空心圆 canvas = cv2.circle(canvas, (100, 100), 80, (0, 0, 255), -1) canvas = cv2.circle(canvas, (250, 250), 80, (0, 255, 255), -1) canvas = cv2.circle(canvas, (400, 400), 80, (0, 255, 0), -1) canvas = cv2.circle(canvas, (400, 100), 80, (0, 0, 0), 5) cv2.imshow("TrafficLights", canvas) # 显示画布 cv2.waitKey() cv2.destroyAllWindows()
3.2 绘制同心圆
import numpy as np import cv2 canvas = np.zeros((600, 600, 3), np.uint8) center_X = int(canvas.shape[1] / 2) center_Y = int(canvas.shape[0] / 2) for r in range(0, 300, 30): cv2.circle(canvas, (center_X, center_Y), r, (0, 255, 0), 5) cv2.imshow("Circles", canvas) cv2.waitKey() cv2.destroyAllWindows()
代码效果如下
3.3 绘制随机圆
import numpy as np import cv2 canvas = np.zeros((600, 600, 3), np.uint8) for numbers in range(0, 100): center_X = np.random.randint(0, high=600) center_Y = np.random.randint(0, high=600) radius = np.random.randint(10, high=150) color = np.random.randint(0, high=256, size=(3,)).tolist() cv2.circle(canvas, (center_X, center_Y), radius, color, -1) cv2.imshow("Circles", canvas) cv2.waitKey() cv2.destroyAllWindows()
效果展示如下:
4. cv2.polylines() 绘制多边形
语法
polylines(img, pts, isClosed, color, thickness=None, lineType=None, shift=None)
- img 画布/图像数组
- pts 是一个列表,列表内是一个shape为(n,2)的numpy数组:形如[[a, b], [c, d], [e, f], [g, h], …],存放的是多边形每个顶点的坐标。
- isClosed 是否闭合。没有默认值,必须手动传入。如果为True则表示是一个闭合的多边形;如果为False则表示不闭合。
import numpy as np import cv2 canvas = np.ones((500, 500, 3), np.uint8) * 255 pts = np.array([[100, 50], [250, 50], [350, 250], [100, 300], [50, 100]], np.int32) # 注意,pts是一个列表,数组的外边还要加一组括号才可正常运行。 canvas = cv2.polylines(canvas, [pts], True, (0, 255, 255), 5) cv2.imshow("Polylines", canvas) cv2.waitKey() cv2.destroyAllWindows()