python基于opencv 实现图像时钟


Posted in Python onJanuary 04, 2021

解决方案详解

python基于opencv 实现图像时钟

绘制表盘

表盘上只有60条分/秒刻线和12条小时刻线,当然还有表盘的外部轮廓圆,也就是重点在如何画72根线。先把简单的圆画出来:

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α

python基于opencv 实现图像时钟

先只考虑将坐标系原点移动到左上角,角度依然是平面坐标系中的逆时针计算,那么新坐标是:
x=r+r×cosα
y=r+r×sinα
对于60条分/秒刻线,刻线间的夹角是360°/60=6°,对于小时刻线,角度是360°/12=30°,这样就得到了72组起点坐标,那怎么得到终点坐标呢?其实同样的原理,用一个同心的小圆来计算得到B点:

python基于opencv 实现图像时钟

通过A/B两点就可以画出直线:

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)


# 到这里基本的表盘图就已经画出来了

python基于opencv 实现图像时钟

角度换算

接下来算是一个小难点,首先时钟的起始坐标在正常二维坐标系的90°方向,其次时钟跟图像一样,都是顺时针计算角度的,所以三者需要统一下:

python基于opencv 实现图像时钟

因为角度是完全对称的,顺逆时针没有影响,所以平面坐标系完全不用理会,放在这里只是便于大家理解。对于时钟坐标和图像坐标,时钟0的0°对应图像的270°,时钟15的90°对应图像的360°,时钟30的180°对应图像的450°(360°+90°)…
所以两者之间的关系便是:
计算角度 = 时钟角度+270°
计算角度 = 计算角度 if 计算角度<=360° else 计算角度-360°

同步时间

Python中如何获取当前时间和添加日期文字都比较简单,看代码就行,我就不解释了。

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 实现图像时钟的详细内容,更多关于python opencv 实现图像时钟的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python实现的一个火车票转让信息采集器
Jul 09 Python
python实现在sqlite动态创建表的方法
May 08 Python
Python爬虫之模拟知乎登录的方法教程
May 25 Python
python 中的int()函数怎么用
Oct 17 Python
Python中if elif else及缩进的使用简述
May 31 Python
解决python xlrd无法读取excel文件的问题
Dec 25 Python
python中update的基本使用方法详解
Jul 17 Python
DjangoWeb使用Datatable进行后端分页的实现
May 18 Python
keras中epoch,batch,loss,val_loss用法说明
Jul 02 Python
Python3爬虫中关于Ajax分析方法的总结
Jul 10 Python
Python3爬虫关于代理池的维护详解
Jul 30 Python
opencv python 对指针仪表读数识别的两种方式
Jan 14 Python
python基于opencv实现人脸识别
Jan 04 #Python
利用python绘制正态分布曲线
Jan 04 #Python
Python 打印自己设计的字体的实例讲解
Jan 04 #Python
Python关于拓扑排序知识点讲解
Jan 04 #Python
Python经典五人分鱼实例讲解
Jan 04 #Python
Python约瑟夫生者死者小游戏实例讲解
Jan 04 #Python
python邮件中附加文字、html、图片、附件实现方法
Jan 04 #Python
You might like
php网页后退不再出现过期
2007/03/08 PHP
Excel数据导入Mysql数据库的实现代码
2008/06/05 PHP
PHP 网页过期时间的控制代码
2009/06/29 PHP
Discuz Uchome ajaxpost小技巧
2011/01/04 PHP
php后台如何避免用户直接进入方法实例
2013/10/15 PHP
WordPress的主题编写中获取头部模板和底部模板
2015/12/28 PHP
php基于session锁防止阻塞请求的方法分析
2017/08/07 PHP
php实现映射操作实例详解
2019/10/02 PHP
JQery jstree 大数据量问题解决方法
2010/03/09 Javascript
使用javascript实现有效时间的控制,并显示将要过期的时间
2014/01/02 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
jQuery寻找n以内完全数的方法
2015/06/24 Javascript
loading动画特效小结
2017/01/22 Javascript
在 Angular2 中实现自定义校验指令(确认密码)的方法
2017/01/23 Javascript
Vue.js学习教程之列表渲染详解
2017/05/17 Javascript
微信小程序实现单个或多个倒计时功能
2020/11/01 Javascript
原生JS实现pc端轮播图效果
2020/12/21 Javascript
python脚本设置超时机制系统时间的方法
2016/02/21 Python
使用Python对微信好友进行数据分析
2018/06/27 Python
TensorFlow基于MNIST数据集实现车牌识别(初步演示版)
2019/08/05 Python
pytorch 在sequential中使用view来reshape的例子
2019/08/20 Python
关于Python Tkinter Button控件command传参问题的解决方式
2020/03/04 Python
在python中使用nohup命令说明
2020/04/16 Python
ASP.NET Core中的配置详解
2021/02/05 Python
python爬取股票最新数据并用excel绘制树状图的示例
2021/03/01 Python
eharmony澳大利亚:网上约会服务
2020/02/29 全球购物
青年教师师德演讲稿
2014/08/26 职场文书
同学聚会通知书
2015/04/20 职场文书
初中毕业感言300字
2015/07/31 职场文书
2016大学生社会实践单位评语
2015/12/01 职场文书
2016教师年度考核评语大全
2015/12/01 职场文书
再读《皇帝的新衣》的读后感悟!
2019/08/07 职场文书
Golang实现AES对称加密的过程详解
2021/05/20 Golang
python基于机器学习预测股票交易信号
2021/05/25 Python
浅谈Python数学建模之线性规划
2021/06/23 Python
详解Go语言中配置文件使用与日志配置
2022/06/01 Golang