濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python識(shí)別圍棋定位棋盤(pán)位置

python識(shí)別圍棋定位棋盤(pán)位置

熱門(mén)標(biāo)簽:西青語(yǔ)音電銷機(jī)器人哪家好 無(wú)錫智能外呼系統(tǒng)好用嗎 成都呼叫中心外呼系統(tǒng)哪家強(qiáng) 電梯新時(shí)達(dá)系統(tǒng)外呼顯示e 百應(yīng)電話機(jī)器人總部 宿州電話機(jī)器人哪家好 地圖標(biāo)注與注銷 南昌地圖標(biāo)注 旅游廁所地圖標(biāo)注怎么弄

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

效果圖

原圖

中間處理效果

最終結(jié)果

思路分析

我們利用python opencv的相關(guān)函數(shù)進(jìn)行操作實(shí)現(xiàn),根據(jù)棋盤(pán)顏色的特征,尋找到相關(guān)特征,將棋盤(pán)區(qū)域摳出來(lái)。最好從原始圖像中將棋盤(pán)位置截取出來(lái)。

源碼:定位棋盤(pán)位置

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"
    # 確認(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 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"
    # 確認(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í)別圍棋定位棋盤(pán)位置的文章就介紹到這了,更多相關(guān)python 圍棋定位棋盤(pán)位置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 基于python定位棋子位置及識(shí)別棋子顏色

標(biāo)簽:許昌 濰坊 七臺(tái)河 雅安 西安 贛州 渭南 辛集

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python識(shí)別圍棋定位棋盤(pán)位置》,本文關(guān)鍵詞  python,識(shí)別,圍棋,定位,棋盤(pán),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python識(shí)別圍棋定位棋盤(pán)位置》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python識(shí)別圍棋定位棋盤(pán)位置的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    龙岩市| 靖江市| 涪陵区| 山阴县| 上饶市| 泸州市| 雷山县| 剑阁县| 新郑市| 尤溪县| 武清区| 黔西| 潞西市| 昌宁县| 静安区| 共和县| 萝北县| 东兰县| 抚顺县| 通江县| 花莲县| 诸暨市| 余姚市| 确山县| 元阳县| 峨山| 南郑县| 资中县| 珠海市| 青岛市| 新乐市| 乐业县| 樟树市| 八宿县| 修水县| 观塘区| 华安县| 南木林县| 兴国县| 平舆县| 蓬安县|