主要是查看方便,相关的见
import cv2
import numpy as np
"""
np.zeros()方法构造了一个300*300的NumPy数组,
同时分配了三个颜色空间,分别表示Red,Green,Blue,
正如zeros名字所描述的一样,这个方法用0填充了
这个数组的每一个元素。在np.zeros()的第二个变量是数据类型
:dtype。由于我们需要用RGB格式来表示我们的图像,
它的取值范围是[0,255],所以我们用“uint8”就显得至关重要了,
如果不声明的话np.zeros()默认的变量类型是float64.
"""
canvas = np.zeros((300, 300, 3), dtype="uint8")
green = (0, 255, 0) #4
cv2.line(canvas, (0, 0), (300, 300), green) #5
cv2.imshow("Canvas", canvas) #6
cv2.waitKey(0) #7
red = (0, 0, 255) #8 OpenCV是BGR模式
cv2.line(canvas, (300, 0), (0, 300), red, 3) #9该线的线型为3个像素
cv2.imshow("Canvas", canvas) #10
cv2.waitKey(0) #11
cv2.rectangle(canvas, (10, 10), (60, 60), green) #12
cv2.imshow("Canvas", canvas) #13
cv2.waitKey(0) #14
cv2.rectangle(canvas, (50, 200), (200, 225), red, 5) #15
cv2.imshow("Canvas", canvas) #16
cv2.waitKey(0) #17
blue = (255, 0, 0) #18
cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1) #19设置线型的参数设置为“-1”,我们便可以得到一个填充蓝色的矩形。
cv2.imshow("Canvas", canvas) #20
cv2.waitKey(0) #21
#画圆形
(centerX, centerY) = (canvas.shape[1] // 2, canvas.shape[0] // 2) #23
white = (255, 255, 255) #24
for r in range(0, 175, 25): #25
cv2.circle(canvas, (centerX, centerY), r, white) #26
cv2.imshow("Canvas", canvas) #27
cv2.waitKey(0) #28
for i in range(0, 25): #29
radius = np.random.randint(5, high=200) #30
color = np.random.randint(0, high=256, size=(3,)) #31
pt = np.random.randint(0, high=300, size=(2,)) #32
cv2.circle(canvas, tuple(pt), radius, color, 1) #33
cv2.imshow("Canvas", canvas) #34
cv2.waitKey(0) #35