搜索
Hi~登录注册
查看: 802|回复: 0

python识别围棋定位棋盘位置

[复制链接]

0

主题

0

帖子

10

积分

新手上路

Rank: 1

积分
10
发表于 2021-8-7 10:25:51 | 显示全部楼层 |阅读模式
目录


  • 效果图
  • 思路分析
  • 源码:定位棋盘位置
最近必要做一个围棋识别的项目,首先要将棋盘位置定位出来,效果图如下:

效果图

原图

中间处理效果

最终结果


思路分析

我们使用python opencv的相关函数进行操纵实现,根据棋盘颜色的特性,寻找到相关特性,将棋盘区域抠出来。最好从原始图像中将棋盘位置截取出来。

源码:定位棋盘位置

[code]from PIL import ImageGrabimport numpy as npimport cv2from glob import globimglist = 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 epsilonmaxarea:            maxarea = cv2.contourArea(c)            maxint = i        i+=1    #多边形拟合    epsilon = 0.02*cv2.arcLength(contours[maxint],True)    if epsilon
回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 点我注册

快速回复 返回顶部 返回列表