import numpy as np
import cv2
import matplotlib.pyplot as plt
def show(image):
plt.imshow(image) #用以显示图片的函数
plt.axis('off') #matplotlib显示函数,默认会显示坐标轴,调用此函数不让其显示坐标轴
plt.show()
image = np.zeros((300,300,3),dtype='uint8') #定义一个图片,显示为全黑
show(image)
画线
green = (0,255,0)
cv2.line(image, (0,0), (300,300), green)
show(image)
blue = (0,0,255)
cv2.line(image, (300,0), (150,150), blue, 5) #5 线的宽度 line函数画线
show(image)
画矩形
red = (255,0,0)
cv2.rectangle(image, (10,10), (60,60), red, 2) #rectangle 矩形函数
show(image)
cv2.rectangle(image, (50,50), (100,100), blue, 5)
show(image)
cv2.rectangle(image, (50,200), (220,280), green, -1) #-1 填充
show(image)
画圆形
image = np.zeros((300,300,3),dtype='uint8')
(cX, cY) = image.shape[1]//2, image.shape[0]//2
white = (255,255,255)
for r in range(0,151,15): #r代表圆的半径 以中间点为圆心
cv2.circle(image, (cX,cY), r, white, 2)
show(image)
image = np.zeros((300,300,3),dtype='uint8')
for i in range(10):
# 半径取值
radius = np.random.randint(5,200)
# 颜色取值
color = np.random.randint(0,255,size=(3,)).tolist() #tolist变为list
# 圆心取值
pt = np.random.randint(0,300,size=(2,))
# 画图
cv2.circle(image, tuple(pt), radius, color, -1)
show(image)