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实现的一个火车票转让信息采集器
Jul 09 Python
详解Python中的日志模块logging
Jun 19 Python
独特的python循环语句
Nov 20 Python
python万年历实现代码 含运行结果
May 20 Python
Python中矩阵创建和矩阵运算方法
Aug 04 Python
Python实现的建造者模式示例
Aug 06 Python
Python实现的多进程拷贝文件并显示百分比功能示例
Apr 09 Python
PyQt5实现从主窗口打开子窗口的方法
Jun 19 Python
python-django中的APPEND_SLASH实现方法
Jun 21 Python
Python 多线程,threading模块,创建子线程的两种方式示例
Sep 29 Python
python3.x中安装web.py步骤方法
Jun 23 Python
Python 没有main函数的原因
Jul 10 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
php中计算时间差的几种方法
2009/12/31 PHP
php判断当前操作系统类型
2015/10/28 PHP
双冒号 ::在PHP中的使用情况
2015/11/05 PHP
PHP之十六个魔术方法详细介绍
2016/11/01 PHP
简单实现PHP留言板功能
2016/12/21 PHP
Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解
2019/10/11 PHP
jquery中通过父级查找进行定位示例
2013/06/28 Javascript
js正则表达式中test,exec,match方法的区别说明
2014/01/29 Javascript
Extjs grid panel自带滚动条失效的解决方法
2014/09/11 Javascript
JavaScript实现梯形乘法表的方法
2015/04/25 Javascript
Node.js利用js-xlsx处理Excel文件的方法详解
2017/07/05 Javascript
vue按需加载组件webpack require.ensure的方法
2017/12/13 Javascript
Bootstrap 按钮样式与使用代码详解
2018/12/09 Javascript
[01:00:26]Ti4主赛事胜者组第一天 EG vs NEWBEE 1
2014/07/19 DOTA
浅谈Python中用datetime包进行对时间的一些操作
2016/06/23 Python
Python构建网页爬虫原理分析
2017/12/19 Python
浅析python3中的os.path.dirname(__file__)的使用
2018/08/30 Python
Django 路由系统URLconf的使用
2018/10/11 Python
python实现图片彩色转化为素描
2019/01/15 Python
python调用百度地图WEB服务API获取地点对应坐标值
2019/01/16 Python
python代理工具mitmproxy使用指南
2019/07/04 Python
如何理解Python中的变量
2020/06/01 Python
Python文件名匹配与文件复制的实现
2020/12/11 Python
恒华伟业笔试面试题
2015/02/26 面试题
大学生优秀的自我评价分享
2013/10/22 职场文书
历史专业毕业生的自我鉴定
2013/11/15 职场文书
酒店秘书求职信范文
2014/02/17 职场文书
2014年作风建设工作总结
2014/10/29 职场文书
2014年园林绿化工作总结
2014/12/11 职场文书
六一领导慰问欢迎词
2015/01/26 职场文书
2015年医务人员医德医风自我评价
2015/03/03 职场文书
2015年七一建党节活动方案
2015/05/05 职场文书
实践论读书笔记
2015/06/29 职场文书
《自己去吧》教学反思
2016/02/16 职场文书
经典励志格言:每日一句,让你每天充满能量
2019/08/16 职场文书
python 实现定时任务的四种方式
2021/04/01 Python