如何利用Python实现围棋的棋盘位置定位
Admin 2022-09-19 群英技术资讯 795 次浏览
这篇文章主要讲解了“如何利用Python实现围棋的棋盘位置定位”,文中的讲解内容简单、清晰、详细,对大家学习或是工作可能会有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。原图

中间处理效果

最终结果

我们利用python opencv的相关函数进行操作实现,根据棋盘颜色的特征,寻找到相关特征,将棋盘区域抠出来。最好从原始图像中将棋盘位置截取出来。
from PIL import ImageGrab
import numpy as np
import cv2
from glob import glob
imglist = sorted(glob("screen/*.jpg"))
for i in imglist:
# while 1:
img = cv2.imread(i)
image = img.copy()
w,h,c = img.shape
img2 = np.zeros((w,h,c), np.uint8)
img3 = np.zeros((w,h,c), np.uint8)
# img = ImageGrab.grab() #bbox specifies specific region (bbox= x,y,width,height *starts top-left)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower = np.array([10,0,0])
upper = np.array([40,255,255])
mask = cv2.inRange(hsv,lower,upper)
erodeim = cv2.erode(mask,None,iterations=2) # 腐蚀
dilateim = cv2.dilate(erodeim,None,iterations=2)
img = cv2.bitwise_and(img,img,mask=dilateim)
frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow("0",img)
i = 0
maxarea = 0
nextarea = 0
maxint = 0
for c in contours:
if cv2.contourArea(c)>maxarea:
maxarea = cv2.contourArea(c)
maxint = i
i+=1
#多边形拟合
epsilon = 0.02*cv2.arcLength(contours[maxint],True)
if epsilon<1:
continue
#多边形拟合
approx = cv2.approxPolyDP(contours[maxint],epsilon,True)
[[x1,y1]] = approx[0]
[[x2,y2]] = approx[2]
checkerboard = image[y1:y2,x1:x2]
cv2.imshow("1",checkerboard)
cv2.waitKey(1000)
cv2.destroyAllWindows()
带保存图像
from PIL import ImageGrab
import numpy as np
import cv2
from glob import glob
import os
imglist = sorted(glob("screen/*.jpg"))
a=0
for i in imglist:
# while 1:
a=a+1
img = cv2.imread(i)
image = img.copy()
w,h,c = img.shape
img2 = np.zeros((w,h,c), np.uint8)
img3 = np.zeros((w,h,c), np.uint8)
# img = ImageGrab.grab() #bbox specifies specific region (bbox= x,y,width,height *starts top-left)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower = np.array([10,0,0])
upper = np.array([40,255,255])
mask = cv2.inRange(hsv,lower,upper)
erodeim = cv2.erode(mask,None,iterations=2) # 腐蚀
dilateim = cv2.dilate(erodeim,None,iterations=2)
img = cv2.bitwise_and(img,img,mask=dilateim)
frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, dst = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 保存图片的地址
img_file_1 = "./temp"
# 确认上述地址是否存在
if not os.path.exists(img_file_1):
os.mkdir(img_file_1)
cv2.imshow("0",img)
cv2.imwrite(img_file_1 + "/" + 'temp_%d.jpg'%a, img)
i = 0
maxarea = 0
nextarea = 0
maxint = 0
for c in contours:
if cv2.contourArea(c)>maxarea:
maxarea = cv2.contourArea(c)
maxint = i
i+=1
#多边形拟合
epsilon = 0.02*cv2.arcLength(contours[maxint],True)
if epsilon<1:
continue
#多边形拟合
approx = cv2.approxPolyDP(contours[maxint],epsilon,True)
[[x1,y1]] = approx[0]
[[x2,y2]] = approx[2]
checkerboard = image[y1:y2,x1:x2]
cv2.imshow("1",checkerboard)
cv2.waitKey(1000)
# 保存图片的地址
img_file_2 = "./checkerboard"
# 确认上述地址是否存在
if not os.path.exists(img_file_2):
os.mkdir(img_file_2)
cv2.imwrite(img_file_2 + "/" + 'checkerboard_%d.jpg'%a, checkerboard)
cv2.destroyAllWindows()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
今天带大家学习怎么用python实现一个无界面的小型图书管理系统,文中有非常详细的图文解说及代码示例,对正在学习python的小伙伴们有很好地帮助,需要的朋友可以参考下
本文主要介绍了使用spyder3调试python程序的实现步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章介绍了Python网络编程之ftplib模块,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了python如何实现数组元素两两相加,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
当因为语法需要但不希望执行任何命令或代码时,则可以使用pass语句。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008