python opencv练习
- 自定义一张[512, 512, 3]的图像
- 在上面写写字,画画圈和矩形
- 显示
代码为:
1 import cv2
2 import numpy as np
3
4 img = np.zeros([512, 512, 3], dtype=np.uint8)
5 for i in range(512):
6 for j in range(512):
7 img[i, j, :] = [i % 256, j % 256, (i + j) % 256]
8
9 cv2.rectangle(img, (200, 200), (400, 400), (0, 0, 255), 3)
10 cv2.circle(img, (300, 300), 100, (0, 255, 255), 3)
11
12 info = 'Hello World'
13 font_face = cv2.FONT_HERSHEY_COMPLEX
14 font_scale = 2
15 thickness = 2
16 text_size = cv2.getTextSize(info, font_face, font_scale, thickness)
17 print(text_size)
18 p_center = (int(512 / 2 - text_size[0][0] / 2), int(512 / 2 - text_size[0][1] / 2))
19 cv2.putText(img, info, p_center, font_face, font_scale, (0, 0, 0), thickness)
20
21 cv2.imshow('demo', img)
22 cv2.waitKey(0)
23 cv2.destroyAllWindows()
显示如下: