python微信跳一跳系列之自动计算跳一跳距离


Posted in Python onFebruary 26, 2018

到现在为止,我们通过前面几篇博文的描述和分析,已经可以自动实现棋子、棋盘位置的准确判断,计算一下两个中心点之间的距离,并绘制在图形上,效果如下。

效果

python微信跳一跳系列之自动计算跳一跳距离

图中的棋子定位采用HSV颜色识别,棋盘定位采用轮廓分割的方法获得,感兴趣的同学可以对其它的定位方法自行验证。

代码

# -*- coding: utf-8 -*-
#VS2017+python3.6+opencv3.4
#2018.02.03
#作者:艾克思

import cv2 
import numpy as np
import math

def hsv(frame):
 lower_blue = np.array([115,75,75]) #设定蓝色的阈值
 upper_blue = np.array([130,255,125])
 r=0 #初始半径=0
 x,y=0,0
 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #转到HSV空间
 mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
 cnts = cv2.findContours(mask_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] 
 if len(cnts) > 0: 
 c = max(cnts, key = cv2.contourArea) #找到面积最大的轮廓
 ((x, y), radius) = cv2.minEnclosingCircle(c) #确定面积最大的轮廓的外接圆 
 center=(int(x),int(y))
 return center

def thresh(img):
 x,y,w,h,x1,y1,w1,h1,x2,y2,w2,h2=0,0,0,0,0,0,0,0,0,0,0,0
 gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 #gray=cv2.GaussianBlur(gray,(13,13),0)#转化为灰度图
 h0,w0=img.shape[:2]
 top=gray[h0//3,1]
 bottom= gray[h0*2//3,1]
 #min_vale=min(top,bottom)
 #max_vale=max(top,bottom)

 thresh1 = cv2.threshold(gray,top,255, cv2.THRESH_BINARY)[1]
 thresh2 = cv2.threshold(gray,175,255, cv2.THRESH_BINARY_INV)[1] 
 img1=thresh1[h0//3:h0*2//3,0:w0]
 img2=thresh2[h0//3:h0*2//3,0:w0]

 cnts1, hierarchy1, rr1 = cv2.findContours(img1,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
 cnts2, hierarchy2, rr2 = cv2.findContours(img2,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

 aim1=0
 y_min=h0//3
 for c in hierarchy1:
 if hierarchy1==None:
  x1,y1,w1,h1=w0//2,h0//3,w0//3,h0//3
  break
 else:
  x,y,w,h = cv2.boundingRect(c)
  if y<=y_min:
  y_min=y
  aim1=c
  x1,y1,w1,h1 = cv2.boundingRect(aim1)
 #cv2.rectangle(img,(x1,y1+h0//3),(x1+w1,y1+h1+h0//3),(0,0,255),2)

 aim2=0
 y_min=h0//3
 for c in hierarchy2:
 if hierarchy2==None:
  x2,y2,w2,h2=w0//2,h0//3,w0//3,h0//3
  break
 else:
  x,y,w,h = cv2.boundingRect(c)
  if y<=y_min:
  y_min=y
  aim2=c
  x2,y2,w2,h2 = cv2.boundingRect(aim2)
 #cv2.rectangle(img,(x2,y2+h0//3),(x2+w2,y2+h2+h0//3),(0,255,255),2)

 if y1+h1//2<=y2+h2//2:
 x,y,w,h=x1,y1,w1,h1
 else: x,y,w,h=x2,y2,w2,h2

 cv2.imshow('img1',thresh1)
 cv2.imshow('img2',thresh2) 

 return (x+w//2,y+h0//3+h//2)

def length(pt1,pt2):
 x1,y1=pt1
 x2,y2=pt2
 length=math.sqrt((x2-x1)**2+(y2-y1)**2)
 return int(length)

def main():
 filepath='e:/python/jump/hsv/007.png'
 video='e:/python/jump/blackwhite/jumpnew.avi'
 cap = cv2.VideoCapture(video) 
 ret=cap.isOpened()
 ret=True
 while ret:
 #ret,img=cap.read() #读入帧
 img=cv2.imread(filepath)
 if not ret:cv2.waitKey(0)
 point1=hsv(img)
 point2=thresh(img)
 len=length(point1,point2)
 cv2.circle(img,point1,3,(0,0,255),-1)
 cv2.circle(img,point1,15,(0,0,255),2)
 cv2.circle(img,point2,3,(0,0,255),-1)
 cv2.circle(img,point2,15,(0,0,255),2)
 cv2.line(img,point1,point2,(255,255,255),2)
 cv2.putText(img, '{}'.format(len) ,(point2[0]-10,point2[1]-20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2,cv2.LINE_8, 0)
 cv2.imshow('img',img)
 #cv2.imwrite(filepath,img)
 cv2.waitKey(0)
 cap.release()
 cv2.destroyAllWindows()

if __name__=='__main__':
 main()

更多内容大家可以参考专题《微信跳一跳》进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python3.3教程之模拟百度登陆代码分享
Jan 16 Python
Python中你应该知道的一些内置函数
Mar 31 Python
Python zip()函数用法实例分析
Mar 17 Python
用Python读取几十万行文本数据
Dec 24 Python
Python+AutoIt实现界面工具开发过程详解
Aug 07 Python
通过python扫描二维码/条形码并打印数据
Nov 14 Python
Python面向对象之私有属性和私有方法应用案例分析
Dec 31 Python
Python使用jupyter notebook查看ipynb文件过程解析
Jun 02 Python
python golang中grpc 使用示例代码详解
Jun 03 Python
django models里数据表插入数据id自增操作
Jul 15 Python
Python 中 Shutil 模块详情
Nov 11 Python
python使用BeautifulSoup 解析HTML
Apr 24 Python
python微信跳一跳系列之色块轮廓定位棋盘
Feb 26 #Python
tensorflow入门之训练简单的神经网络方法
Feb 26 #Python
基于Python实现的微信好友数据分析
Feb 26 #Python
TensorFlow 实战之实现卷积神经网络的实例讲解
Feb 26 #Python
使用django-crontab实现定时任务的示例
Feb 26 #Python
Django中的CBV和FBV示例介绍
Feb 25 #Python
python中正则表达式的使用方法
Feb 25 #Python
You might like
怎么样可以把 phpinfo()屏蔽掉?
2006/11/24 PHP
让你的网站首页自动选择语言转跳
2006/12/06 PHP
PHP通过header实现文本文件下载的代码
2010/08/08 PHP
zend framework框架中url大小写问题解决方法
2014/08/19 PHP
thinkphp浏览历史功能实现方法
2014/10/29 PHP
round robin权重轮循算法php实现代码
2016/05/28 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
2019/05/08 PHP
探讨在JQuery和Js中,如何让ajax执行完后再继续往下执行
2013/07/09 Javascript
js关于字符长度限制的问题示例探讨
2014/01/24 Javascript
js运动动画的八个知识点
2015/03/12 Javascript
JQuery中ajax方法访问web服务实例
2015/07/18 Javascript
JS基于cookie实现来宾统计记录访客信息的方法
2015/08/04 Javascript
JS实现网页上随机产生超链接地址的方法
2015/11/09 Javascript
将JavaScript的jQuery库中表单转化为JSON对象的方法
2015/11/17 Javascript
使用postMesssage()实现iframe跨域页面间的信息传递
2016/03/29 Javascript
nodejs如何获取时间戳与时间差
2016/08/03 NodeJs
Angularjs实现mvvm式的选项卡示例代码
2016/09/08 Javascript
详谈AngularJs 控制器、数据绑定、作用域
2017/07/09 Javascript
JS获取一个表单字段中多条数据并转化为json格式
2017/10/17 Javascript
ajax请求+vue.js渲染+页面加载的示例
2018/02/11 Javascript
详解Angular6 热加载配置方案
2018/08/18 Javascript
将RGB值转换为灰度值的简单算法
2019/10/09 Javascript
Antd的table组件表格的序号自增操作
2020/10/27 Javascript
Python入门之modf()方法的使用
2015/05/15 Python
Python实现多线程HTTP下载器示例
2017/02/11 Python
python将邻接矩阵输出成图的实现
2019/11/21 Python
Jupyter notebook命令和编辑模式常用快捷键汇总
2020/11/17 Python
教师作风整改措施思想汇报
2014/10/12 职场文书
2015年安置帮教工作总结
2015/05/22 职场文书
热血教师观后感
2015/06/10 职场文书
重阳节活动主持词
2015/07/04 职场文书
2015年机关作风和效能建设工作总结
2015/07/23 职场文书
企业安全生产规章制度
2015/08/06 职场文书
你会写报告?产品体验报告到底该怎么写?
2019/08/14 职场文书
2020年元旦祝福语录,总有适合你的
2019/12/31 职场文书
OpenCV-Python实现图像平滑处理操作
2021/06/08 Python