python识别围棋定位棋盘位置


Posted in Python onJuly 26, 2021

最近需要做一个围棋识别的项目,首先要将棋盘位置定位出来,效果图如下:

效果图

原图

python识别围棋定位棋盘位置

中间处理效果

python识别围棋定位棋盘位置

最终结果

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()

到此这篇关于python识别围棋定位棋盘位置的文章就介绍到这了,更多相关python 围棋定位棋盘位置内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python删除指定目录下过期文件的2个脚本分享
Apr 10 Python
python实现将html表格转换成CSV文件的方法
Jun 28 Python
举例讲解Python中字典的合并值相加与异或对比
Jun 04 Python
在Django中输出matplotlib生成的图片方法
May 24 Python
如何利用python制作时间戳转换工具详解
Sep 12 Python
实例讲解python中的协程
Oct 08 Python
python简单鼠标自动点击某区域的实例
Jun 25 Python
Python socket 套接字实现通信详解
Aug 27 Python
python 实现绘制整齐的表格
Nov 18 Python
Python 实现将数组/矩阵转换成Image类
Jan 09 Python
Python class的继承方法代码实例
Feb 14 Python
PyTorch中的拷贝与就地操作详解
Dec 09 Python
python之基数排序的实现
Jul 26 #Python
python之PySide2安装使用及QT Designer UI设计案例教程
python代码实现备忘录案例讲解
Jul 26 #Python
python之django路由和视图案例教程
Jul 26 #Python
OpenCV图像变换之傅里叶变换的一些应用
Python类方法总结讲解
pandas数值排序的实现实例
Jul 25 #Python
You might like
日本十大惊悚动漫
2020/03/04 日漫
BBS(php &amp; mysql)完整版(一)
2006/10/09 PHP
浅析PHP水印技术
2007/02/14 PHP
php正则表达式(regar expression)
2011/09/10 PHP
PHP file_exists问题杂谈
2012/05/07 PHP
php实现refresh刷新页面批量导入数据的方法
2014/12/23 PHP
CentOS 上搭建 PHP7 开发测试环境
2017/02/26 PHP
实例讲解YII2中多表关联的使用方法
2017/07/21 PHP
解决在Laravel 中处理OPTIONS请求的问题
2019/10/11 PHP
基于Jquery的简单&amp;简陋Tabs插件代码
2010/02/09 Javascript
基于JQuery的数字改变的动画效果--可用来做计数器
2010/08/11 Javascript
循环 vs 递归浅谈
2013/02/28 Javascript
让js弹出窗口居前显示的实现方法
2013/07/10 Javascript
jQuery中阻止冒泡事件的方法介绍
2014/04/12 Javascript
原生js jquery ajax请求以及jsonp的调用方法
2017/08/04 jQuery
原生javascript AJAX 三级联动的实现代码
2018/05/04 Javascript
jQuery实现基本淡入淡出效果的方法详解
2018/09/05 jQuery
three.js实现圆柱体
2018/12/30 Javascript
深入理解使用Vue实现Context-Menu的思考与总结
2019/03/09 Javascript
JavaScript实现多张图片放大镜效果示例【不限定图片尺寸,rem单位】
2019/05/14 Javascript
javascript实现前端input密码输入强度验证
2020/06/24 Javascript
[05:45]Ti4观战指南(下)
2014/07/07 DOTA
python创建列表并给列表赋初始值的方法
2015/07/28 Python
2018年Python值得关注的开源库、工具和开发者(总结篇)
2018/01/04 Python
python3.7环境下安装Anaconda的教程图解
2019/09/10 Python
Python响应对象text属性乱码解决方案
2020/03/31 Python
CSS3打造百度贴吧的3D翻牌效果示例
2017/01/04 HTML / CSS
《猴子种果树》教学反思
2014/04/26 职场文书
财务人员担保书
2014/05/13 职场文书
计划生育证明格式及范本
2014/10/09 职场文书
党建工作汇报材料
2014/12/24 职场文书
法制教育观后感
2015/06/17 职场文书
班干部学习委员竞选稿
2015/11/20 职场文书
使用goaccess分析nginx日志的详细方法
2021/07/09 Servers
HTML中的表格元素介绍
2022/02/28 HTML / CSS
Golang 切片(Slice)实现增删改查
2022/04/22 Golang