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爬虫之使用urllib2组件抓取网页内容
Nov 04 Python
Python实现对比不同字体中的同一字符的显示效果
Apr 23 Python
Python下rrdtool模块的基本使用方法
Nov 13 Python
Python闭包的两个注意事项(推荐)
Mar 20 Python
Python实现的简单模板引擎功能示例
Sep 02 Python
Python 实现网页自动截图的示例讲解
May 17 Python
python实现定时压缩指定文件夹发送邮件
Dec 22 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
Aug 06 Python
Django获取该数据的上一条和下一条方法
Aug 12 Python
python numpy存取文件的方式
Apr 01 Python
Python 连接 MySQL 的几种方法
Sep 09 Python
python3代码中实现加法重载的实例
Dec 03 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实现网页内容html标签补全和过滤的方法小结【2种方法】
2017/04/27 PHP
PHP使用递归按层级查找数据的方法
2019/11/10 PHP
使用jquery与图片美化checkbox和radio控件的代码(打包下载)
2010/11/11 Javascript
JS将表单导出成EXCEL的实例代码
2013/11/11 Javascript
JavaScript DOM节点添加示例
2014/07/16 Javascript
node.js中的console.log方法使用说明
2014/12/09 Javascript
animate 实现滑动切换效果【实例代码】
2016/05/05 Javascript
vue2实现移动端上传、预览、压缩图片解决拍照旋转问题
2017/04/13 Javascript
Angular客户端请求Rest服务跨域问题的解决方法
2017/09/19 Javascript
JS Testing Properties 判断属性是否在对象里的方法
2017/10/01 Javascript
基于vue2.0实现仿百度前端分页效果附实现代码
2018/10/30 Javascript
swiper实现导航滚动效果
2020/12/13 Javascript
[43:36]Liquid vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Python 自动安装 Rising 杀毒软件
2009/04/24 Python
python类型强制转换long to int的代码
2013/02/10 Python
分享15个最受欢迎的Python开源框架
2014/07/13 Python
django定期执行任务(实例讲解)
2017/11/03 Python
扩展Django admin的list_filter()可使用范围方法
2019/08/21 Python
python的json中方法及jsonpath模块用法分析
2019/12/06 Python
pytorch 数据处理:定义自己的数据集合实例
2019/12/31 Python
Pytorch技巧:DataLoader的collate_fn参数使用详解
2020/01/08 Python
解决Pytorch 加载训练好的模型 遇到的error问题
2020/01/10 Python
对Tensorflow中Device实例的生成和管理详解
2020/02/04 Python
Python利用Xpath选择器爬取京东网商品信息
2020/06/01 Python
使用html5实现表格实现标题合并的实例代码
2019/05/13 HTML / CSS
学生励志演讲稿
2014/01/06 职场文书
父亲八十大寿答谢词
2014/01/23 职场文书
运动会入场词60字
2014/02/15 职场文书
水利水电建筑施工应届生求职信
2014/07/04 职场文书
安全生产月宣传标语
2014/10/06 职场文书
清洁工工作总结
2015/08/11 职场文书
远程教育培训心得体会
2016/01/09 职场文书
优质护理服务心得体会
2016/01/22 职场文书
vue项目两种方式实现竖向表格的思路分析
2021/04/28 Vue.js
原生Javascript+HTML5一步步实现拖拽排序
2021/06/12 Javascript
Windows Server 2016服务器用户管理及远程授权图文教程
2022/08/14 Servers