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 装饰器功能以及函数参数使用介绍
Jan 27 Python
Python3里的super()和__class__使用介绍
Apr 23 Python
Python实现LRU算法的2种方法
Jun 24 Python
Python的净值数据接口调用示例分享
Mar 15 Python
Python黑魔法@property装饰器的使用技巧解析
Jun 16 Python
基于python 字符编码的理解
Sep 02 Python
python正则表达式面试题解答
Apr 28 Python
将python文件打包成EXE应用程序的方法
May 22 Python
python 有效的括号的实现代码示例
Nov 11 Python
Python实现非正太分布的异常值检测方式
Dec 09 Python
Python接口测试数据库封装实现原理
May 09 Python
Python快速优雅的批量修改Word文档样式
May 20 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
生成sessionid和随机密码的例子
2006/10/09 PHP
apache mysql php 源码编译使用方法
2012/05/03 PHP
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
2014/03/17 PHP
PHP实例分享判断客户端是否使用代理服务器及其匿名级别
2014/06/04 PHP
Laravel (Lumen) 解决JWT-Auth刷新token的问题
2019/10/24 PHP
Javascript日期对象的dateAdd与dateDiff方法
2008/11/18 Javascript
鼠标滑在标题上显示图片的JS代码
2013/11/19 Javascript
JS的encodeURI和java的URLDecoder.decode使用介绍
2014/05/08 Javascript
javascript获取当前鼠标坐标的方法
2015/01/10 Javascript
浅谈javascript构造函数与实例化对象
2015/06/22 Javascript
javascript用正则表达式过滤空格的实现代码
2016/06/14 Javascript
Vue.js实例方法之生命周期详解
2017/07/03 Javascript
详解angularjs获取元素以及angular.element()用法
2017/07/25 Javascript
详解Angular2学习笔记之Html属性绑定
2018/01/03 Javascript
浅谈使用mpvue开发小程序需要注意和了解的知识点
2018/05/23 Javascript
webpack实践之DLLPlugin 和 DLLReferencePlugin的使用教程
2019/06/10 Javascript
vue keep-alive 动态删除组件缓存的例子
2019/11/04 Javascript
JavaScript使用canvas绘制随机验证码
2020/02/17 Javascript
在Python的Django框架中创建语言文件
2015/07/27 Python
Python进程间通信 multiProcessing Queue队列实现详解
2019/09/23 Python
Python matplotlib 绘制双Y轴曲线图的示例代码
2020/06/12 Python
Python 如何实现访问者模式
2020/07/28 Python
html5 canvas实现给图片添加平铺水印
2019/08/20 HTML / CSS
英国领先的运动营养品牌:Protein Dynamix
2018/01/02 全球购物
沙特阿拉伯网上购物:Sayidaty Mall
2018/05/06 全球购物
JustFab加拿大:女鞋、靴子、手袋和服装在线
2018/05/18 全球购物
CK加拿大官网:Calvin Klein加拿大
2020/03/14 全球购物
公证书标准格式
2014/04/10 职场文书
大学专科求职信
2014/07/02 职场文书
幼儿园中班教师个人工作总结
2015/02/06 职场文书
应届毕业生的自我评价
2019/06/21 职场文书
Golang的继承模拟实例
2021/06/30 Golang
node.js如何自定义实现一个EventEmitter
2021/07/16 Javascript
从原生JavaScript到React深入理解
2022/07/23 Javascript
Python创建SQL数据库流程逐步讲解
2022/09/23 Python
HTML中link标签属性的具体用法
2023/05/07 HTML / CSS