时间:2021-05-22
到现在为止,我们通过前面几篇博文的描述和分析,已经可以自动实现棋子、棋盘位置的准确判断,计算一下两个中心点之间的距离,并绘制在图形上,效果如下。
效果
图中的棋子定位采用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()更多内容大家可以参考专题《微信跳一跳》进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
随着微信跳一跳的走红,近日快手很多网红主播也在玩一款叫做“快手跳一跳”的游戏。快手跳一跳基本玩法与微信跳一跳一样,只是花样更多,感兴
微信跳一跳自动代码,具体内容如下那个跳一跳python“外挂”,有几个python文件,其中有一个是得到截图,然后鼠标在图片上点击两次,python窗口上会打印
跳一跳这款微信小游戏相信最近有很多朋友下班路上都在玩,小编发现最近很多玩家都在问跳一跳排名多久会清除,微信跳一跳的规律是什么呢?下面开淘小编为大家找到了相关
万众期待的微信小游戏“跳一跳”商业化之路终于开启。今天,微信小游戏“跳一跳”上线了又一广告。腾讯微信一位高管也在朋友圈证实此事。大叔做个快评。以“跳一跳”为首的
还记得小游戏“跳一跳”吗?作为微信平台上的第一款小游戏,跳一跳一上线就火遍大江南北。耐克曾狂砸2000万广告费拿下“跳一跳”首个广告;今年4月,跳一跳举办了一场