python識(shí)別圍棋定位棋盤位置
最近需要做一個(gè)圍棋識(shí)別的項(xiàng)目,首先要將棋盤位置定位出來(lái),效果圖如下:
效果圖
原圖

中間處理效果

最終結(jié)果

思路分析
我們利用python opencv的相關(guān)函數(shù)進(jìn)行操作實(shí)現(xiàn),根據(jù)棋盤顏色的特征,尋找到相關(guān)特征,將棋盤區(qū)域摳出來(lái)。最好從原始圖像中將棋盤位置截取出來(lái)。
源碼:定位棋盤位置
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"
# 確認(rèn)上述地址是否存在
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"
# 確認(rèn)上述地址是否存在
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()
到此這篇關(guān)于python識(shí)別圍棋定位棋盤位置的文章就介紹到這了,更多相關(guān)python 圍棋定位棋盤位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python利用memory_profiler實(shí)現(xiàn)內(nèi)存分析
memory_profiler是第三方模塊,用于監(jiān)視進(jìn)程的內(nèi)存消耗以及python程序內(nèi)存消耗的逐行分析。本文將利用memory_profiler實(shí)現(xiàn)內(nèi)存分析,需要的可以參考一下2022-10-10
python學(xué)習(xí)開(kāi)發(fā)mock接口
這篇文章主要為大家詳細(xì)介紹了python學(xué)習(xí)開(kāi)發(fā)mock接口的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Python while 循環(huán)使用的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇Python while 循環(huán)使用的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
使用Python進(jìn)行數(shù)據(jù)可視化實(shí)現(xiàn)引人注目的視覺(jué)效果
這篇文章主要介紹了使用Python進(jìn)行數(shù)據(jù)可視化實(shí)現(xiàn)引人注目的視覺(jué)效果,您將了解基本的數(shù)據(jù)可視化概念,以及如何創(chuàng)建各種引人注目的圖表和圖形,從而更好地理解和呈現(xiàn)數(shù)據(jù)2023-04-04
python實(shí)現(xiàn)跨域代理服務(wù)器的方法
這篇文章主要介紹了python實(shí)現(xiàn)跨域代理服務(wù)器的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
如何優(yōu)雅地處理Django中的favicon.ico圖標(biāo)詳解
默認(rèn)情況下,瀏覽器訪問(wèn)一個(gè)網(wǎng)站的時(shí)候,同時(shí)還會(huì)向服務(wù)器請(qǐng)求"/favicon.ico"這個(gè)URL,目的是獲取網(wǎng)站的圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅地處理Django中favicon.ico圖標(biāo)的相關(guān)資料,需要的朋友可以參考下2018-07-07

