画图的几个基本函数:画线,画矩形,画圆
cv2.line, cv2.rectangle, and cv2.circle
1. 一个300*300的画布,画一条线。从左上角到右下
import numpy as np
import cv2# Initialize our canvas as a 300x300 with 3 channels,
# Red, Green, and Blue, with a black background
canvas = np.zeros((300, 300, 3), dtype = "uint8")# Draw a green line from the top-left corner of our canvas
# to the bottom-right
green = (0, 255, 0)
cv2.line(canvas, (0, 0), (300, 300), green)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
2. 再画一根线,红色,线宽3个像素,从左下角到右上角
# Now, draw a 3 pixel thick red line from the top-right
# corner to the bottom-left
red = (0, 0, 255)
cv2.line(canvas, (300, 0), (0, 300), red, 3)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
明显宽了很多有木有
3. 画个矩形吧
# Draw a green 50x50 pixel square, starting at 10x10 and
# ending at 60x60
cv2.rectangle(canvas, (10, 10), (60, 60), green)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
4. 再画两个矩形吧, 注意线宽设置为-1的话就是实心的图形
# Draw another rectangle, this time we'll make it red and
# 5 pixels thick
cv2.rectangle(canvas, (50, 200), (200, 225), red, 5)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)# Let's draw one last rectangle: blue and filled in
blue = (255, 0, 0)
cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1)
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
5. 再来个艺术的,画一堆随机的圆
for i in range(0, 25):# randomly generate a radius size between 5 and 200,# generate a random color, and then pick a random# point on our canvas where the circle will be drawnradius = np.random.randint(5, high = 200)color = np.random.randint(0, high = 256, size = (3,)).tolist()pt = np.random.randint(0, high = 300, size = (2,))# draw our random circlecv2.circle(canvas, tuple(pt), radius, color, -1)# Show our masterpiece
cv2.imshow("Canvas", canvas)
cv2.waitKey(0)
颜色随机,半径随机。但是都是实心的