如何检测上面有碎片的简单2D棋盘?

到目前为止,我的代码可以检测到一个空的棋盘,也可以检测到每个棋子。但是我不知道如何将两者放在一起。

我的目标是能够根据棋盘图像(二维)实时重新创建国际象棋游戏。

该代码不会检测到棋盘上有碎片。

Input image: This is detected fine. Output image:

enter image description here

The above is fine with an empty board. However, findChessboardCorners will return false if I give it the same image as above, except with all pieces on the board.

import cv2
import numpy as np

def main():
    # Chessboard
    img = cv2.imread('/chess_main.png', 1)
    # Board detection
    ret, corners = cv2.findChessboardCorners(img, (7,7), None)
    print(ret)
    img = cv2.drawChessboardCorners(img, (7,7), corners, ret)

    cv2.imshow('detected', img)
    cv2.waitKey(0)

    img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)
    # Piece detection:
    # Thresholds to detect the pieces correctly (w = white, b = black)
    piece_threshold = {'bk': 0.5, 'bq': 0.6, 'br': 0.55, 'bb': 0.7, 'bn': 0.7, 'bp': 0.8,
                        'wk': 0.7, 'wq': 0.6, 'wr': 0.4, 'wb': 0.5, 'wn': 0.5, 'wp': 0.5}

    for piece, threshold in piece_threshold.items():
        # Loop over each piece
        piece_filename = '/{}.png'.format(piece)
        piece_template = cv2.imread(piece_filename, 0)
        width, height = piece_template.shape[::-1]
        res_piece = cv2.matchTemplate(img_gray, piece_template,cv2.TM_CCOEFF_NORMED)
        loc = np.where(res_piece >= threshold)
        # Draw rectangle around matching piece
        for pt in zip(*loc[::-1]):
            cv2.rectangle(img_rgb,pt,(pt[0]+width, pt[1]+height),(0,255,255),1)

    # Resize image
    img = img_rgb
    scale_percent = 50
    width = int(img.shape[1] * scale_percent / 100)
    height = int(img.shape[0] * scale_percent / 100)
    dim = (width, height)
    resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
    cv2.imshow('detected', resized)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

main()

有没有一种方法可以将空板映射到上面有碎片的板? 我不太确定下一步应该怎么做。我需要以某种方式建立董事会的坐标。

任何人都可以帮助建立董事会吗?

评论