解決方案詳解
繪制表盤
表盤上只有60條分/秒刻線和12條小時刻線,當然還有表盤的外部輪廓圓,也就是重點在如何畫72根線。先把簡單的圓畫出來:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import cv2 as cv import math import datetime import numpy as np margin = 5 # 上下左右邊距 radius = 220 # 圓的半徑 center = (center_x, center_y) = ( 225 , 225 ) # 圓心 # 1. 新建一個畫板并填充成白色 img = np.zeros(( 450 , 450 , 3 ), np.uint8) img[:] = ( 255 , 255 , 255 ) # 2. 畫出圓盤 cv.circle(img, center, radius, ( 0 , 0 , 0 ), thickness = 5 ) |
我們使用opencv畫直線的時候,需知道直線的起點和終點坐標,那么畫72根線就變成了獲取72組坐標。
平面坐標系下,已知半徑和角度的話,a點的坐標可以表示為:
x=r×cosα
y=r×sinα
先只考慮將坐標系原點移動到左上角,角度依然是平面坐標系中的逆時針計算,那么新坐標是:
x=r+r×cosα
y=r+r×sinα
對于60條分/秒刻線,刻線間的夾角是360°/60=6°,對于小時刻線,角度是360°/12=30°,這樣就得到了72組起點坐標,那怎么得到終點坐標呢?其實同樣的原理,用一個同心的小圓來計算得到b點:
通過a/b兩點就可以畫出直線:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
pt1 = [] # 3. 畫出60條秒和分鐘的刻線 for i in range ( 60 ): # 最外部圓,計算a點 x1 = center_x + (radius - margin) * math.cos(i * 6 * np.pi / 180.0 ) y1 = center_y + (radius - margin) * math.sin(i * 6 * np.pi / 180.0 ) pt1.append(( int (x1), int (y1))) # 同心小圓,計算b點 x2 = center_x + (radius - 15 ) * math.cos(i * 6 * np.pi / 180.0 ) y2 = center_y + (radius - 15 ) * math.sin(i * 6 * np.pi / 180.0 ) cv.line(img, pt1[i], ( int (x2), int (y2)), ( 0 , 0 , 0 ), thickness = 2 ) # 4. 畫出12條小時的刻線 for i in range ( 12 ): # 12條小時刻線應該更長一點 x = center_x + (radius - 25 ) * math.cos(i * 30 * np.pi / 180.0 ) y = center_y + (radius - 25 ) * math.sin(i * 30 * np.pi / 180.0 ) # 這里用到了前面的pt1 cv.line(img, pt1[i * 5 ], ( int (x), int (y)), ( 0 , 0 , 0 ), thickness = 5 ) # 到這里基本的表盤圖就已經畫出來了 |
角度換算
接下來算是一個小難點,首先時鐘的起始坐標在正常二維坐標系的90°方向,其次時鐘跟圖像一樣,都是順時針計算角度的,所以三者需要統一下:
因為角度是完全對稱的,順逆時針沒有影響,所以平面坐標系完全不用理會,放在這里只是便于大家理解。對于時鐘坐標和圖像坐標,時鐘0的0°對應圖像的270°,時鐘15的90°對應圖像的360°,時鐘30的180°對應圖像的450°(360°+90°)…
所以兩者之間的關系便是:
計算角度 = 時鐘角度+270°
計算角度 = 計算角度 if 計算角度<=360° else 計算角度-360°
同步時間
python中如何獲取當前時間和添加日期文字都比較簡單,看代碼就行,我就不解釋了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
while ( 1 ): # 不斷拷貝表盤圖,才能更新繪制,不然會重疊在一起 temp = np.copy(img) # 5. 獲取系統時間,畫出動態的時-分-秒三條刻線 now_time = datetime.datetime.now() hour, minute, second = now_time.hour, now_time.minute, now_time.second # 畫秒刻線 # 參見博客,opencv中的角度是順時針計算的,所以需要轉換下 sec_angle = second * 6 + 270 if second < = 15 else (second - 15 ) * 6 sec_x = center_x + (radius - margin) * math.cos(sec_angle * np.pi / 180.0 ) sec_y = center_y + (radius - margin) * math.sin(sec_angle * np.pi / 180.0 ) cv.line(temp, center, ( int (sec_x), int (sec_y)), ( 255 , 0 , 0 ), 2 ) # 畫分刻線 min_angle = minute * 6 + 270 if minute < = 15 else (minute - 15 ) * 6 min_x = center_x + (radius - 35 ) * math.cos(min_angle * np.pi / 180.0 ) min_y = center_y + (radius - 35 ) * math.sin(min_angle * np.pi / 180.0 ) cv.line(temp, center, ( int (min_x), int (min_y)), ( 0 , 255 , 0 ), 8 ) # 畫時刻線 hour_angle = hour * 30 + 270 if hour < = 3 else (hour - 3 ) * 30 hour_x = center_x + (radius - 75 ) * math.cos(hour_angle * np.pi / 180.0 ) hour_y = center_y + (radius - 75 ) * math.sin(hour_angle * np.pi / 180.0 ) cv.line(temp, center, ( int (hour_x), int (hour_y)), ( 0 , 0 , 255 ), 20 ) # 6. 添加當前日期文字 font = cv.font_hershey_simplex time_str = now_time.strftime( "%d/%m/%y" ) cv.puttext(img, time_str, ( 135 , 275 ), font, 1 , ( 0 , 0 , 0 ), 2 ) cv.imshow( 'clocking' , temp) if cv.waitkey( 1 ) = = 27 : # 按下esc鍵退出 break |
以上就是python基于opencv 實現圖像時鐘的詳細內容,更多關于python opencv 實現圖像時鐘的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/wojianxin/p/12626164.html