脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python识别围棋定位棋盘位置

python识别围棋定位棋盘位置

2021-12-15 00:31翟羽嚄 Python

最近需要做一个围棋识别的项目,本文就介绍了棋盘位置定位,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

效果图

原图

python识别围棋定位棋盘位置

中间处理效果

python识别围棋定位棋盘位置

最终结果

python识别围棋定位棋盘位置

思路分析

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

源码:定位棋盘位置

  1. from PIL import ImageGrab 
  2. import numpy as np 
  3. import cv2 
  4. from glob import glob 
  5.  
  6. imglist = sorted(glob("screen/*.jpg")) 
  7. for i in imglist: 
  8. # while 1: 
  9.     img = cv2.imread(i) 
  10.     image = img.copy() 
  11.     w,h,c = img.shape 
  12.     img2 =  np.zeros((w,h,c), np.uint8) 
  13.     img3 =  np.zeros((w,h,c), np.uint8) 
  14.     # img = ImageGrab.grab() #bbox specifies specific region (bbox= x,y,width,height *starts top-left) 
  15.      
  16.  
  17.     hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) 
  18.     lower = np.array([10,0,0]) 
  19.     upper = np.array([40,255,255]) 
  20.     mask = cv2.inRange(hsv,lower,upper) 
  21.     erodeim = cv2.erode(mask,None,iterations=2)  # 腐蚀  
  22.     dilateim = cv2.dilate(erodeim,None,iterations=2)  
  23.  
  24.     img = cv2.bitwise_and(img,img,mask=dilateim) 
  25.     frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
  26.     ret, dst = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY) 
  27.     contours,hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
  28.  
  29.  
  30.     cv2.imshow("0",img) 
  31.     i = 0 
  32.     maxarea = 0 
  33.     nextarea = 0 
  34.     maxint = 0 
  35.     for c in contours: 
  36.         if cv2.contourArea(c)>maxarea: 
  37.             maxarea = cv2.contourArea(c) 
  38.             maxint = i 
  39.         i+=1 
  40.  
  41.     #多边形拟合 
  42.     epsilon = 0.02*cv2.arcLength(contours[maxint],True) 
  43.     if epsilon<1: 
  44.         continue 
  45.      
  46.     #多边形拟合 
  47.     approx = cv2.approxPolyDP(contours[maxint],epsilon,True) 
  48.     [[x1,y1]] = approx[0] 
  49.     [[x2,y2]] = approx[2] 
  50.  
  51.     checkerboard = image[y1:y2,x1:x2] 
  52.     cv2.imshow("1",checkerboard) 
  53.     cv2.waitKey(1000) 
  54.  
  55. cv2.destroyAllWindows() 

带保存图像

  1. from PIL import ImageGrab 
  2. import numpy as np 
  3. import cv2 
  4. from glob import glob 
  5. import os 
  6.  
  7. imglist = sorted(glob("screen/*.jpg")) 
  8. a=0 
  9. for i in imglist: 
  10. # while 1: 
  11.     a=a+1 
  12.     img = cv2.imread(i) 
  13.     image = img.copy() 
  14.     w,h,c = img.shape 
  15.     img2 =  np.zeros((w,h,c), np.uint8) 
  16.     img3 =  np.zeros((w,h,c), np.uint8) 
  17.     # img = ImageGrab.grab() #bbox specifies specific region (bbox= x,y,width,height *starts top-left) 
  18.      
  19.  
  20.     hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) 
  21.     lower = np.array([10,0,0]) 
  22.     upper = np.array([40,255,255]) 
  23.     mask = cv2.inRange(hsv,lower,upper) 
  24.     erodeim = cv2.erode(mask,None,iterations=2)  # 腐蚀  
  25.     dilateim = cv2.dilate(erodeim,None,iterations=2)  
  26.  
  27.     img = cv2.bitwise_and(img,img,mask=dilateim) 
  28.     frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
  29.     ret, dst = cv2.threshold(frame, 100, 255, cv2.THRESH_BINARY) 
  30.     contours,hierarchy = cv2.findContours(dst, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
  31.  
  32.     # 保存图片的地址 
  33.     img_file_1 = "./temp" 
  34.     # 确认上述地址是否存在 
  35.     if not os.path.exists(img_file_1): 
  36.         os.mkdir(img_file_1) 
  37.  
  38.     cv2.imshow("0",img) 
  39.     cv2.imwrite(img_file_1 + "/" + 'temp_%d.jpg'%a, img) 
  40.     i = 0 
  41.     maxarea = 0 
  42.     nextarea = 0 
  43.     maxint = 0 
  44.     for c in contours: 
  45.         if cv2.contourArea(c)>maxarea: 
  46.             maxarea = cv2.contourArea(c) 
  47.             maxint = i 
  48.         i+=1 
  49.  
  50.     #多边形拟合 
  51.     epsilon = 0.02*cv2.arcLength(contours[maxint],True) 
  52.     if epsilon<1: 
  53.         continue 
  54.      
  55.     #多边形拟合 
  56.     approx = cv2.approxPolyDP(contours[maxint],epsilon,True) 
  57.     [[x1,y1]] = approx[0] 
  58.     [[x2,y2]] = approx[2] 
  59.  
  60.     checkerboard = image[y1:y2,x1:x2] 
  61.     cv2.imshow("1",checkerboard) 
  62.     cv2.waitKey(1000) 
  63.     # 保存图片的地址 
  64.     img_file_2 = "./checkerboard" 
  65.     # 确认上述地址是否存在 
  66.     if not os.path.exists(img_file_2): 
  67.         os.mkdir(img_file_2) 
  68.     cv2.imwrite(img_file_2 + "/" + 'checkerboard_%d.jpg'%a, checkerboard) 
  69. cv2.destroyAllWindows() 

到此这篇关于python识别围棋定位棋盘位置的文章就介绍到这了,更多相关python 围棋定位棋盘位置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mao_hui_fei/article/details/118363347

延伸 · 阅读

精彩推荐