欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python識別圍棋定位棋盤位置

 更新時間:2021年07月26日 10:44:57   作者:翟羽嚄  
最近需要做一個圍棋識別的項目,本文就介紹了棋盤位置定位,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近需要做一個圍棋識別的項目,首先要將棋盤位置定位出來,效果圖如下:

效果圖

原圖

在這里插入圖片描述

中間處理效果

在這里插入圖片描述

最終結果

在這里插入圖片描述

思路分析

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

源碼:定位棋盤位置

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"
    # 確認上述地址是否存在
    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"
    # 確認上述地址是否存在
    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 圍棋定位棋盤位置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python中range函數(shù)的使用方法

    Python中range函數(shù)的使用方法

    這篇文章主要介紹了Python中range函數(shù)的使用方法,文章基于Python3環(huán)境展開range函數(shù)的使用方法,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • 詳解Python中最常用的10個內置函數(shù)

    詳解Python中最常用的10個內置函數(shù)

    Python作為一種多用途編程語言,擁有豐富的內置函數(shù)庫,這些函數(shù)可以極大地提高開發(fā)效率,本文將介紹Python中最常用的10個內置函數(shù),我們將深入了解每個函數(shù),并提供示例代碼以幫助您更好地理解它們,需要的朋友可以參考下
    2023-11-11
  • 利用Python實現(xiàn)學生信息管理系統(tǒng)的完整實例

    利用Python實現(xiàn)學生信息管理系統(tǒng)的完整實例

    這篇文章主要給大家介紹了關于如何利用Python實現(xiàn)學生信息管理系統(tǒng)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Python實現(xiàn)一行代碼自動繪制藝術畫

    Python實現(xiàn)一行代碼自動繪制藝術畫

    DiscoArt?是一個很牛的開源模塊,它能根據(jù)你給定的關鍵詞自動繪畫。本文就將利用這一模塊實現(xiàn)一行代碼自動繪制藝術畫,需要的可以參考一下
    2022-12-12
  • Selenium(Python web測試工具)基本用法詳解

    Selenium(Python web測試工具)基本用法詳解

    這篇文章主要介紹了Selenium(Python web測試工具)基本用法,結合實例形式分析了Selenium的基本安裝、簡單使用方法及相關操作技巧,需要的朋友可以參考下
    2018-08-08
  • python的set處理二維數(shù)組轉一維數(shù)組的方法示例

    python的set處理二維數(shù)組轉一維數(shù)組的方法示例

    這篇文章主要介紹了python的set處理二維數(shù)組轉一維數(shù)組的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Python3+Appium安裝使用教程

    Python3+Appium安裝使用教程

    這篇文章主要介紹了Python3+Appium安裝使用教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • Python編程快速上手——Excel表格創(chuàng)建乘法表案例分析

    Python編程快速上手——Excel表格創(chuàng)建乘法表案例分析

    這篇文章主要介紹了Python Excel表格創(chuàng)建乘法表,結合具體實例形式分析了Python接受cmd命令操作Excel文件創(chuàng)建乘法表相關實現(xiàn)技巧,需要的朋友可以參考下
    2020-02-02
  • python實現(xiàn)的人臉識別打卡系統(tǒng)

    python實現(xiàn)的人臉識別打卡系統(tǒng)

    這篇文章主要介紹了python實現(xiàn)的人臉識別打卡系統(tǒng),幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-05-05
  • Python 數(shù)據(jù)庫操作 SQLAlchemy的示例代碼

    Python 數(shù)據(jù)庫操作 SQLAlchemy的示例代碼

    這篇文章主要介紹了Python 數(shù)據(jù)庫操作 SQLAlchemy的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02

最新評論