import numpy as np
import cv2
import matplotlib.pyplot as plt
def show(image):
plt.imshow(image)
plt.axis('off')
plt.show()
image = np.zeros((300,300,3),dtype='uint8')
#可以看成一张图片,三通道的、长宽为300*300,里面像素全为0
show(image)#显示图片
#画线
green = (0,255,0)
cv2.line(image,(0,0),(300,300),green)
#画一条从0,0到300,300的一条绿色的线
show(image)
blue = (0,0,255)
cv2.line(image,(300,0),(150,150),blue,5)
#画一条蓝色的从300,0到150,150,粗细度为5的线
#之前绿色的没用指定粗细,默认值为1
show(image)
#画矩形
red = (255,0,0)
cv2.rectangle(image,(10,10),(60,60),red,2)
#画矩形,第一个点为矩形左上方的坐标,第二个为右下方坐标
#red为指定颜色,2为指定矩形框宽度
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)
#shape[0]是图片的高度。shape[1]是图片的宽度
for r in range(0,151,15):
cv2.circle(image,(cX,cY),r,white,2)
#第一个坐标是圆中心坐标点,r是指圆的半径
#while是指圆线条颜色,2代表圆的线的宽度
show(image)
image = np.zeros((300,300,3),dtype='uint8')
for i in range(10):
#半径取值,为从5到200的一个随机值
radius = np.random.randint(5,200)
#颜色取值,从0到255随机取
#size是因为是取三通道的数值,后面的tolist是表示把它变成一个list
color = np.random.randint(0,255,size=(3,)).tolist()
#圆心取值,size等于2是因为圆心坐标点需要两个数x和y
pt = np.random.randint(0,300,size=(2,))
#画图,依次为圆心,半径、颜色、填充
cv2.circle(image,tuple(pt),radius,color,-1)
show(image)
#这个函数每次执行会得到不一样的图,因为都是随机的