脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python微信跳一跳系列之自动计算跳一跳距离

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

2021-01-18 00:10艾克思工作室 Python

这篇文章主要为大家详细介绍了python微信跳一跳系列之自动计算跳一跳距离,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

效果

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

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

代码

python" id="highlighter_145612">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/m0_37606112/article/details/79248699

延伸 · 阅读

精彩推荐