国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python微信跳一跳系列之自動計算跳一跳距離

2020-02-22 23:19:47
字體:
來源:轉載
供稿:網友

到現在為止,我們通過前面幾篇博文的描述和分析,已經可以自動實現棋子、棋盤位置的準確判斷,計算一下兩個中心點之間的距離,并繪制在圖形上,效果如下。

效果

這里寫圖片描述

圖中的棋子定位采用HSV顏色識別,棋盤定位采用輪廓分割的方法獲得,感興趣的同學可以對其它的定位方法自行驗證。

代碼

# -*- coding: utf-8 -*-#VS2017+python3.6+opencv3.4#2018.02.03#作者:艾克思import cv2 import numpy as npimport mathdef 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 centerdef 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()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望江县| 信宜市| 沙河市| 罗城| 分宜县| 永修县| 锡林浩特市| 兴安盟| 合水县| 灵宝市| 洛扎县| 安国市| 黑龙江省| 西平县| 滨州市| 民县| 金坛市| 佛学| 福泉市| 铜鼓县| 房山区| 霍林郭勒市| 清河县| 大丰市| 新和县| 邯郸县| 无锡市| 泰兴市| 磐石市| 平武县| 元谋县| 枣阳市| 通河县| 阿拉尔市| 肥城市| 寻乌县| 色达县| 甘谷县| 方山县| 台南市| 清涧县|