14CV画基本图形
Tommy老师视频链接
# 1.导入库
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 2.定义: 颜色(字典)
colors = {
'blue': (255, 0, 0),
'green': (0, 255, 0),
'red': (0, 0, 255),
'yellow': (0, 255, 255),
'magenta': (255, 0, 255),
'cyan': (255, 255, 0),
'white': (255, 255, 255),
'black': (255, 0, 0),
'gray': (125, 125, 125),
'rand': np.random.randint(0, high=256, size=(3,)).tolist(),
'dark_gray': (50, 50, 50),
'light_gray': (220, 220, 220),
}
# 3.定义: 显示图像
def show_image(image, title):
img_RGB = image[:, :, ::-1]
plt.title(title)
plt.imshow(img_RGB)
plt.show()
# 4.创建画布
canva1 = np.zeros((400, 400, 3), np.uint8) # 默认背景色为黑色
canva1[:] = colors['white'] # 注释掉就是黑底
show_image(canva1, 'Background')
# 5.画直线 cv2.line()
cv2.line(canva1, (0, 0), (400, 400), colors['magenta'], 10)
cv2.line(canva1, (0, 400), (400, 0), colors['cyan'], 5)
show_image(canva1, 'cv2.line')
# 6.创建画布
canva2 = np.zeros((400, 400, 3), np.uint8) # 默认背景色为黑色
canva2[:] = colors['white'] # 注释掉就是黑底
show_image(canva2, 'Background')
# 7.画长方形 cv2.rectangle()
cv2.rectangle(canva2, (10, 50), (70, 120), colors['gray'], 5)
cv2.rectangle(canva2, (150, 50), (200, 300), colors['light_gray'], -1) # -1 内填充
show_image(canva2, 'cv2.rectangle')
# 8.创建画布
canva3 = np.zeros((400, 400, 3), np.uint8) # 默认背景色为黑色
canva3[:] = colors['white'] # 注释掉就是黑底
show_image(canva3, 'Background')
# 9.画圆形 cv2.circle()
cv2.circle(canva3, (200, 200), 150, colors['green'], 5)
cv2.circle(canva3, (50, 50), 50, colors['red'], -1) # -1 内填充
show_image(canva3, 'cv2.circle')
# 6.创建画布
canva4 = np.zeros((400, 400, 3), np.uint8) # 默认背景色为黑色
canva4[:] = colors['white'] # 注释掉就是黑底
show_image(canva4, 'Background')
# 7.画三角形 cv2.rectangle()
pts = np.array([[250, 5], [220, 80], [280, 80]], np.int32)
pts = pts.reshape((-1, 1, 2)) # 固定写法
pts2 = np.array([[350, 50], [250, 150], [300, 300]], np.int32)
pts2 = pts2.reshape((-1, 1, 2)) # 固定写法
cv2.polylines(canva4, [pts], True, colors['green'], 3) # -1 内填充
cv2.polylines(canva4, [pts2], False, colors['red'], 3) # False 1-3点,不连接
show_image(canva4, 'cv2.polylines')
别的也没啥说的
ok,那就这样吧~
欢迎各位大佬留言吐槽,也可以深入交流~