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操作列表之List.insert()方法的使用
May 20 Python
在Linux下使用Python的matplotlib绘制数据图的教程
Jun 11 Python
python二分查找算法的递归实现方法
May 12 Python
使用Python对SQLite数据库操作
Apr 06 Python
浅谈Python实现Apriori算法介绍
Dec 20 Python
python切片及sys.argv[]用法详解
May 25 Python
influx+grafana自定义python采集数据和一些坑的总结
Sep 17 Python
浅谈python3发送post请求参数为空的情况
Dec 28 Python
python如何制作缩略图
Apr 30 Python
PyCharm导入python项目并配置虚拟环境的教程详解
Oct 13 Python
python实现斗地主分牌洗牌
Jun 22 Python
Python基础 括号()[]{}的详解
Nov 07 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
sqlyog 中文乱码问题的设置方法
2008/10/19 PHP
php中在PDO中使用事务(Transaction)
2011/05/14 PHP
PHP针对JSON操作实例分析
2015/01/12 PHP
用php和jQuery来实现“顶”和“踩”的投票功能
2016/10/13 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
js null,undefined,字符串小结
2010/08/21 Javascript
jquery解析xml字符串简单示例
2014/04/11 Javascript
javascript解三阶幻方(九宫格)
2015/04/22 Javascript
使用控制台破解百小度一个月只准改一次名字
2015/08/13 Javascript
JS实现动态生成表格并提交表格数据向后端
2020/11/25 Javascript
详解angular中如何监控dom渲染完毕
2017/01/03 Javascript
微信小程序-拍照或选择图片并上传文件
2017/01/06 Javascript
基于pako.js实现gzip的压缩和解压功能示例
2017/06/13 Javascript
jQuery选择器_动力节点Java学院整理
2017/07/05 jQuery
详解Vuex中mapState的具体用法
2017/09/28 Javascript
在 Typescript 中使用可被复用的 Vue Mixin功能
2018/04/17 Javascript
快速解决vue动态绑定多个class的官方实例语法无效的问题
2018/09/05 Javascript
angular 服务的单例模式(依赖注入模式下)详解
2018/10/22 Javascript
ElementUI多个子组件表单的校验管理实现
2019/11/07 Javascript
Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
2014/06/10 Python
通过mod_python配置运行在Apache上的Django框架
2015/07/22 Python
浅谈python中的getattr函数 hasattr函数
2016/06/14 Python
Python中二维列表如何获取子区域元素的组成
2017/01/19 Python
Python实现matplotlib显示中文的方法详解
2018/02/06 Python
tensorflow如何批量读取图片
2019/08/29 Python
浅谈Python类中的self到底是干啥的
2019/11/11 Python
解析HTML5中的新功能本地存储localStorage
2016/03/01 HTML / CSS
彪马荷兰官网:PUMA荷兰
2019/05/08 全球购物
美国名牌手表折扣网站:Jomashop
2020/05/22 全球购物
在校生党员自我评价
2013/09/25 职场文书
求职自荐信怎么写
2014/03/06 职场文书
经典演讲稿汇总
2014/05/19 职场文书
中职毕业生自我鉴定
2014/09/13 职场文书
网站文案策划岗位职责
2015/04/14 职场文书
冬季作息时间调整通知
2015/04/24 职场文书
2016年感恩父亲节活动总结
2016/04/01 职场文书