绘制图形和文字

demo1

# 绘制线段
import cv2
import numpy as np
# 创建一个300×300 3通道的图像
canvas = np.ones((300, 300, 3), np.uint8)*255
# 绘制一条直线起点坐标为(50, 50)终点坐标为(250,50),颜色的BGR值为(255, 0, 0)(蓝色),粗细为5
canvas = cv2.line(canvas, (50, 50), (250, 50), (255, 0, 0), 5)

# 绘制一条直线起点坐标为(50, 150)终点坐标为(250,150),颜色的BGR值为(0, 255, 0)(绿色),粗细为10
canvas = cv2.line(canvas, (50, 150), (250, 150), (0, 255, 0), 10)

# 绘制一条直线起点坐标为(50, 250)终点坐标为(250,250),颜色的BGR值为(0, 0, 255)(红色),粗细为15
canvas = cv2.line(canvas, (50, 250), (250, 250), (0, 0, 255), 15)

# 绘制一条直线起点坐标为(150, 50)终点坐标为(150,250),颜色的BGR值为(0, 255, 255)(黄色),粗细为20
canvas = cv2.line(canvas, (150, 50), (150, 250), (0, 255, 255), 5)

cv2.imshow("Lines", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo2

# 绘制矩形
import cv2
import numpy as np

canvas = np.zeros((300, 300, 3), np.uint8)

# 在图像上绘制一个左上角坐标为(50, 50)、右下角坐标为(200, 150)、青色的、线条宽度为20的举行边框,最后一个参数为-1时绘制出一个实心举行
canvas = cv2.rectangle(canvas, (50, 50), (200, 150), (255, 255, 0), 5)
cv2.imshow("img", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo3

# 绘制正方形
import numpy as np
import cv2
canvas = np.zeros((300, 300, 3), np.uint8)
canvas = cv2.rectangle(canvas, (50, 50), (250, 250), (0, 0, 255), 40)
canvas = cv2.rectangle(canvas, (90, 90), (210, 210), (0, 255, 0), 30)
canvas = cv2.rectangle(canvas, (120, 120), (180, 180), (255, 0, 0), 20)
canvas = cv2.rectangle(canvas, (140, 140), (160, 160), (0, 255, 255), -1)
cv2.imshow("img", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo4

# 绘制交通灯
import numpy as np
import cv2

canvas = np.zeros((100, 300, 3), np.uint8)
canvas = cv2.circle(canvas, (50, 50), 40, (0, 0, 255), -1)
canvas = cv2.circle(canvas, (150, 50), 40, (0, 255, 255), -1)
canvas = cv2.circle(canvas, (250, 50), 40, (0, 255, 0), -1)

cv2.imshow("circle", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo5

# 绘制同心圆

import  numpy as np
import cv2

canvas = np.zeros((300, 300, 3), np.uint8)
# 圆心的横坐标等于画布宽度的一半,center_X是圆心的横坐标
center_X = int(canvas.shape[1]/2)
center_Y = int(canvas.shape[0]/2)

for r in range(0, 150, 30):
    cv2.circle(canvas, (center_X, center_Y), r, (0, 255, 0), 5)
cv2.imshow("Circles", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo6

# 绘制27个随机的圆

import numpy as np
import cv2

canvas = np.zeros((300, 300, 3), np.uint8)
for numbers in range(0, 28):
    # 获得随机的圆心横坐标,这个坐标在(0, 299)内取值
    center_X = np.random.randint(0, high=300)
    # 获得随机的圆心纵坐标,这个坐标在(0, 299)内取值
    center_Y = np.random.randint(0, high=300)
    # 获得最忌的半径,这个半径在(11, 70)内取值
    radius = np.random.randint(11,high=71)
    # 获得随机的线条颜色,这个颜色在(0, 255)取值
    color = np.random.randint(0, high=256, size=(3,)).tolist()
    # 绘制一个圆心坐标为(center_X, center_Y)圆
    cv2.circle(canvas, (center_X, center_Y), radius, color, -1)

cv2.imshow("Circles", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo7

# 绘制等腰梯形
import numpy as np
import cv2
canvas = np.zeros((300, 300, 3), np.uint8)

pts = np.array([[100, 50], [200, 50], [50, 250], [250, 250]], np.int32)

canvas = cv2.polylines(canvas, [pts], True, (0, 0, 255), 5)

cv2.imshow("img", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo8

# 绘制文字
import numpy as np
import cv2
canvas = np.zeros((100, 300, 3), np.uint8)
fontStyle = cv2.FONT_HERSHEY_DUPLEX + cv2.FONT_ITALIC
cv2.putText(canvas, "mrsoft", (20, 70), fontStyle, 2, (0, 255, 0), 5)
cv2.imshow("img", canvas)
cv2.waitKey()
cv2.destroyAllWindows()

demo9

import cv2
import time
import numpy as np
# 画面的宽和高
width, height = 200, 200
# 圆半径
r = 20
# 圆心横坐标起始坐标
x = r + 20
# 圆心纵坐标起始坐标
y = r + 100
# 每一帧的移动速度
x_offer = y_offer = 4

while cv2.waitKey(-1) == -1:
    if x > width - r or x < r:
        x_offer *= -1
    if y > height-r or y < r:
        y_offer *= -1
    x += x_offer
    y += y_offer
    img = np.ones((width, height, 3), np.uint8)*255
    cv2.circle(img, (x, y), r, (255, 0, 0), -1)
    cv2.imshow("img", img)
    time.sleep(1/60)
cv2.destroyAllWindows()