在Python3 环境下安装opencv-python 后练习画基本图形:
import numpy as np
import cv2 # BGR format
GREEN = (0, 255, 0)
RED = (0, 0, 255)
BLUE = (255, 0, 0)
WHITE = (255,255,255) canvas = np.zeros((300,300,3), dtype = "uint8")
# 画线,框,⚪
cv2.line(canvas,(0,0), (300,300), GREEN, 2)
# cv2.imshow("Canvas", canvas)
cv2.rectangle(canvas,(10,10), (60,60),GREEN, 1)
cv2.rectangle(canvas,(300,0), (0,300),RED,5)
cv2.rectangle(canvas, (200,55), (55,200), BLUE, -1)
(cx, cy) = (canvas.shape[1]/2, canvas.shape[0]/2)
for r in range(0, 200, 25):
print(cx,cy,r)
cv2.circle(canvas, (int(cx), int(cy)), r, WHITE) cv2.imshow('Canvas', canvas)
cv2.waitKey(0) canvas = np.zeros((300,300,3), dtype = "uint8")
# 随机画⚪
for i in range(0, 25):
r = np.random.randint(5, 200)
color = np.random.randint(0, high = 256, size = (3,)).tolist()
pt = np.random.randint(0, high = 300, size = (2,))
print(r)
print("*" * 25)
print(color)
print("*" * 25)
print(pt)
cv2.circle(canvas, tuple(pt), r, color, -1) cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
运行后可看到以下效果.