1. 画直线 cv2.line()
2. 画长方形 cv2.rectangle()
3. 画圆型 cv2.circle()
4. 画折线 cv2.polylines()
代码实现
import cv2
import numpy as np
import matplotlib.pyplot as plt
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':(0, 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)}
def show_image(image, title):
img_RGB = image[:, :, ::-1]
plt.title(title)
plt.imshow(img_RGB)
plt.show()
canvas = np.zeros((400, 400, 3), np.uint8)
canvas[:] = colors['white']
show_image(canvas, "Background")
cv2.line(canvas, (0, 0), (400, 400),colors['green'], 5)
cv2.line(canvas, (0, 400), (400, 0),colors['black'], 5)
show_image(canvas, "cv2.line()")
canvas = np.zeros((400, 400, 3), np.uint8)
canvas[:] = colors['white']
show_image(canvas, "Background")
cv2.rectangle(canvas, (10, 50),(70, 120), colors['green'], 3)
cv2.rectangle(canvas, (150, 50),(200, 300), colors['blue'], -1)
show_image(canvas, "cv2.rectangle()")
canvas = np.zeros((400, 400, 3), np.uint8)
canvas[:] = colors['white']
show_image(canvas, "Background")
cv2.circle(canvas, (200, 200), 150, colors['black'], 3)
cv2.circle(canvas, (200, 200), 50, colors['green'], -1)
show_image(canvas, "cv2.circle()")
canvas = np.zeros((400, 400, 3), np.uint8)
canvas[:] = colors['white']
show_image(canvas, "Background")
pts = np.array([[250, 5], [220, 80], [280, 80]], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(canvas, [pts], True, colors['green'], 3)
pts2 = np.array([[150, 200], [90, 130], [280, 180]], np.int32)
pts2 = pts2.reshape((-1, 1, 2))
cv2.polylines(canvas, [pts2], False, colors['black'], 3)
show_image(canvas, "cv2.polylines()")