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中使用logging模块代替print(logging简明指南)
Jul 09 Python
python中os操作文件及文件路径实例汇总
Jan 15 Python
Python中实现三目运算的方法
Jun 21 Python
Python实现快速排序和插入排序算法及自定义排序的示例
Feb 16 Python
Python存取XML的常见方法实例分析
Mar 21 Python
使用django-crontab实现定时任务的示例
Feb 26 Python
Python安装图文教程 Pycharm安装教程
Mar 27 Python
利用python脚本如何简化jar操作命令
Feb 24 Python
Python 批量刷博客园访问量脚本过程解析
Aug 30 Python
PyQt5多线程刷新界面防假死示例
Dec 13 Python
python中JWT用户认证的实现
May 18 Python
Python面向对象之内置函数相关知识总结
Jun 24 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
开源SNS系统-ThinkSNS
2008/05/18 PHP
PHP生成网页快照 不用COM不用扩展.
2010/02/11 PHP
php线性表顺序存储实现代码(增删查改)
2012/02/16 PHP
PHP可变函数的使用详解
2013/06/14 PHP
PHP将XML转数组过程详解
2013/11/13 PHP
检测codeigniter脚本消耗内存情况的方法
2015/03/21 PHP
Yii2单元测试用法示例
2016/11/12 PHP
Laravel如何使用Redis共享Session
2018/02/23 PHP
JavaScript函数、方法、对象代码
2008/10/29 Javascript
JQuery 绑定select标签的onchange事件,弹出选择的值,并实现跳转、传参
2011/01/06 Javascript
用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件)
2012/06/22 Javascript
基于promise.js实现nodejs的promises库
2014/07/06 NodeJs
全面解析JavaScript里的循环方法之forEach,for-in,for-of
2020/04/20 Javascript
js实现城市级联菜单的2种方法
2017/06/23 Javascript
JavaScript实现的级联算法示例【省市二级联动功能】
2018/12/25 Javascript
详解关于React-Router4.0跳转不置顶解决方案
2019/05/10 Javascript
玩转python selenium鼠标键盘操作(ActionChains)
2020/04/12 Python
python随机取list中的元素方法
2018/04/08 Python
使用Python和xlwt向Excel文件中写入中文的实例
2018/04/21 Python
python占位符输入方式实例
2019/05/27 Python
Flask配置Cors跨域的实现
2019/07/12 Python
使用Python实现画一个中国地图
2019/11/23 Python
python datetime时间格式的相互转换问题
2020/06/11 Python
英国可持续奢侈品包包品牌:Elvis & Kresse
2018/08/05 全球购物
大学毕业自我鉴定范文
2014/02/03 职场文书
40岁生日感言
2014/02/15 职场文书
老师对学生的寄语
2014/04/09 职场文书
团委书记的竞聘演讲稿
2014/04/24 职场文书
医生爱岗敬业演讲稿
2014/08/26 职场文书
部门群众路线教育实践活动对照检查材料思想汇报
2014/10/07 职场文书
新教师个人工作总结
2015/02/06 职场文书
初中英语教师个人工作总结
2015/02/09 职场文书
产品质量保证书范本
2015/02/27 职场文书
复兴之路纪录片观后感
2015/06/02 职场文书
mysql使用 not int 子查询隐含陷阱
2022/04/12 MySQL
nginx配置指令之server_name的具体使用
2022/08/14 Servers