|
| 1 | +# ex2tron's blog: |
| 2 | +# http://ex2tron.wang |
| 3 | + |
| 4 | +import cv2 |
| 5 | +import math |
| 6 | +import datetime |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +margin = 5 # 上下左右边距 |
| 10 | +radius = 220 # 圆的半径 |
| 11 | +center = (center_x, center_y) = (225, 225) # 圆心 |
| 12 | + |
| 13 | +# 1. 新建一个画板并填充成白色 |
| 14 | +img = np.zeros((450, 450, 3), np.uint8) |
| 15 | +img[:] = (255, 255, 255) |
| 16 | + |
| 17 | +# 2. 画出圆盘 |
| 18 | +cv2.circle(img, center, radius, (0, 0, 0), thickness=5) |
| 19 | + |
| 20 | +pt1 = [] |
| 21 | + |
| 22 | +# 3. 画出60条秒和分钟的刻线 |
| 23 | +for i in range(60): |
| 24 | + # 最外部圆,计算A点 |
| 25 | + x1 = center_x+(radius-margin)*math.cos(i*6*np.pi/180.0) |
| 26 | + y1 = center_y+(radius-margin)*math.sin(i*6*np.pi/180.0) |
| 27 | + pt1.append((int(x1), int(y1))) |
| 28 | + |
| 29 | + # 同心小圆,计算B点 |
| 30 | + x2 = center_x+(radius-15)*math.cos(i*6*np.pi/180.0) |
| 31 | + y2 = center_y+(radius-15)*math.sin(i*6*np.pi/180.0) |
| 32 | + |
| 33 | + cv2.line(img, pt1[i], (int(x2), int(y2)), (0, 0, 0), thickness=2) |
| 34 | + |
| 35 | +# 4. 画出12条小时的刻线 |
| 36 | +for i in range(12): |
| 37 | + # 12条小时刻线应该更长一点 |
| 38 | + x = center_x+(radius-25)*math.cos(i*30*np.pi/180.0) |
| 39 | + y = center_y+(radius-25)*math.sin(i*30*np.pi/180.0) |
| 40 | + # 这里用到了前面的pt1 |
| 41 | + cv2.line(img, pt1[i*5], (int(x), int(y)), (0, 0, 0), thickness=5) |
| 42 | + |
| 43 | + |
| 44 | +# 到这里基本的表盘图就已经画出来了 |
| 45 | + |
| 46 | +while(1): |
| 47 | + # 不断拷贝表盘图,才能更新绘制,不然会重叠在一起 |
| 48 | + temp = np.copy(img) |
| 49 | + |
| 50 | + # 5. 获取系统时间,画出动态的时-分-秒三条刻线 |
| 51 | + now_time = datetime.datetime.now() |
| 52 | + hour, minute, second = now_time.hour, now_time.minute, now_time.second |
| 53 | + |
| 54 | + # 画秒刻线 |
| 55 | + # 参见博客,OpenCV中的角度是顺时针计算的,所以需要转换下 |
| 56 | + sec_angle = second*6+270 if second <= 15 else (second-15)*6 |
| 57 | + sec_x = center_x+(radius-margin)*math.cos(sec_angle*np.pi/180.0) |
| 58 | + sec_y = center_y+(radius-margin)*math.sin(sec_angle*np.pi/180.0) |
| 59 | + cv2.line(temp, center, (int(sec_x), int(sec_y)), (203, 222, 166), 2) |
| 60 | + |
| 61 | + # 画分刻线 |
| 62 | + min_angle = minute*6+270 if minute <= 15 else (minute-15)*6 |
| 63 | + min_x = center_x+(radius-35)*math.cos(min_angle*np.pi/180.0) |
| 64 | + min_y = center_y+(radius-35)*math.sin(min_angle*np.pi/180.0) |
| 65 | + cv2.line(temp, center, (int(min_x), int(min_y)), (186, 199, 137), 8) |
| 66 | + |
| 67 | + # 画时刻线 |
| 68 | + hour_angle = hour*30+270 if hour <= 3 else (hour-3)*30 |
| 69 | + hour_x = center_x+(radius-65)*math.cos(hour_angle*np.pi/180.0) |
| 70 | + hour_y = center_y+(radius-65)*math.sin(hour_angle*np.pi/180.0) |
| 71 | + cv2.line(temp, center, (int(hour_x), int(hour_y)), (169, 198, 26), 15) |
| 72 | + |
| 73 | + # 6. 添加当前日期文字 |
| 74 | + font = cv2.FONT_HERSHEY_SIMPLEX |
| 75 | + time_str = now_time.strftime("%d/%m/%Y") |
| 76 | + cv2.putText(img, time_str, (135, 275), font, 1, (0, 0, 0), 2) |
| 77 | + |
| 78 | + cv2.imshow('clocking', temp) |
| 79 | + if cv2.waitKey(1) == 27: # 按下ESC键退出 |
| 80 | + break |
0 commit comments