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选择排序算法的实现代码
Nov 21 Python
一则python3的简单爬虫代码
May 26 Python
Python 序列化 pickle/cPickle模块使用介绍
Nov 30 Python
python通过shutil实现快速文件复制的方法
Mar 14 Python
wxPython使用系统剪切板的方法
Jun 16 Python
Python实现base64编码的图片保存到本地功能示例
Jun 22 Python
windows下python虚拟环境virtualenv安装和使用详解
Jul 16 Python
python扫描线填充算法详解
Feb 19 Python
大数据分析用java还是Python
Jul 06 Python
Python文件夹批处理操作代码实例
Jul 21 Python
python处理写入数据代码讲解
Oct 22 Python
PyTorch device与cuda.device用法
Apr 03 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实现的XML操作类【XML Library】
2016/12/29 PHP
关于PHP通用返回值设置方法
2017/03/31 PHP
prototype 学习笔记整理
2009/07/17 Javascript
javascript 得到变量类型的函数
2010/05/19 Javascript
JavaScript内核之基本概念
2011/10/21 Javascript
jBox 2.3基于jquery的最新多功能对话框插件 常见使用问题解答
2011/11/10 Javascript
jQuery动态显示和隐藏datagrid中的某一列的方法
2013/12/11 Javascript
浅析基于WEB前端页面的页面内容搜索的实现思路
2014/06/10 Javascript
谈一谈javascript闭包
2016/01/28 Javascript
JS代码防止SQL注入的方法(超简单)
2016/04/12 Javascript
javascript实现根据函数名称字符串动态执行函数的方法示例
2016/12/28 Javascript
javascript Function函数理解与实战
2017/12/01 Javascript
jQuery UI实现动画效果代码分享
2018/08/19 jQuery
JavaScript实现新年倒计时效果
2018/11/17 Javascript
js回调函数原理与用法案例分析
2020/03/04 Javascript
vue自定义树状结构图的实现方法
2020/10/18 Javascript
JS闭包原理及其使用场景解析
2020/12/03 Javascript
Python文件和目录操作详解
2015/02/08 Python
给Python的Django框架下搭建的BLOG添加RSS功能的教程
2015/04/08 Python
Python中的进程分支fork和exec详解
2015/04/11 Python
Python爬虫:通过关键字爬取百度图片
2017/02/17 Python
Python获取好友地区分布及好友性别分布情况代码详解
2019/07/10 Python
python迭代器常见用法实例分析
2019/11/22 Python
解决python使用list()时总是报错的问题
2020/05/05 Python
python 实现压缩和解压缩的示例
2020/09/22 Python
解决python 在for循环并且pop数组的时候会跳过某些元素的问题
2020/12/11 Python
html5+css3之制作header实例与更新
2020/12/21 HTML / CSS
娇韵诗俄罗斯官方网站:Clarins俄罗斯
2020/10/03 全球购物
乌克兰网上珠宝商店:GoldSoveren
2020/03/31 全球购物
化学教学随笔感言
2014/02/19 职场文书
个人授权委托书
2014/09/15 职场文书
大雁塔英文导游词
2015/02/10 职场文书
离婚起诉书范文2016
2015/11/26 职场文书
pytorch 一行代码查看网络参数总量的实现
2021/05/12 Python
用Python将GIF动图分解成多张静态图片
2021/06/11 Python
Redis集群的关闭与重启操作
2021/07/07 Redis