主页 > 知识库 > python识别围棋定位棋盘位置

python识别围棋定位棋盘位置

热门标签:西青语音电销机器人哪家好 无锡智能外呼系统好用吗 成都呼叫中心外呼系统哪家强 电梯新时达系统外呼显示e 百应电话机器人总部 宿州电话机器人哪家好 地图标注与注销 南昌地图标注 旅游厕所地图标注怎么弄

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

效果图

原图

中间处理效果

最终结果

思路分析

我们利用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 epsilon1:
        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 epsilon1:
        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识别围棋定位棋盘位置》,本文关键词  python,识别,围棋,定位,棋盘,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《python识别围棋定位棋盘位置》相关的同类信息!
  • 本页收集关于python识别围棋定位棋盘位置的相关信息资讯供网民参考!
  • 推荐文章