python识别围棋定位棋盘位置
作者:翟羽嚄 时间:2023-01-09 01:49:34
最近需要做一个围棋识别的项目,首先要将棋盘位置定位出来,效果图如下:
效果图
原图
中间处理效果
最终结果
思路分析
我们利用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()
来源:https://blog.csdn.net/mao_hui_fei/article/details/118363347
标签:python,围棋,定位,棋盘位置
0
投稿
猜你喜欢
python实现双人五子棋(终端版)
2022-08-26 14:28:57
Python: glob匹配文件的操作
2023-05-30 00:36:24
python基础之错误和异常处理
2021-07-28 05:46:15
Python 变量类型详解
2022-06-23 00:44:55
linux 安装 mysql 8.0.19 详细步骤及问题解决方法
2024-01-13 22:31:33
sql server数据库最大Id冲突问题解决方法之一
2012-01-05 19:28:42
Python学习之装饰器与类的装饰器详解
2023-11-23 20:04:07
Python中的函数参数传递问题
2023-09-28 05:38:12
Python的Flask站点中集成xhEditor文本编辑器的教程
2023-04-05 00:41:06
OpenCV图像卷积之cv.filter2D()函数详解
2021-11-16 07:51:34
apache和nginx下vue页面刷新404的解决方案
2024-04-26 17:37:16
如何利用Python动态展示排序算法
2022-03-06 17:23:48
Python中变量的输入输出实例代码详解
2022-11-26 00:47:56
matplotlib教程——强大的python作图工具库
2021-05-18 23:42:55
python抓取某汽车网数据解析html存入excel示例
2023-11-02 16:46:35
fso对象CreateTextFile方法调用时“无效的过程调用或参数”错误
2009-05-26 15:39:00
mysql 索引使用及优化详情
2024-01-24 16:06:56
uniapp实现录音上传功能
2024-06-07 16:00:04
完美解决pycharm导入自己写的py文件爆红问题
2023-01-22 10:16:48
Go语言高效编程的3个技巧总结
2024-02-03 02:52:38