'How To Detect There Is A Piece On A Chess Board
I'm Tring To Convert A Chess Board Image Into A Matrix With The Pieces On The Board.
like so:
[
[P, P, P, P, P, P, P, P,]
[P, P, P, P, P, P, P, P,]
[-, -, -, -, -, -, -, -,]
[-, -, -, -, -, -, -, -,]
[P, P, P, P, P, P, P, P,]
[P, P, P, P, P, P, P, P,]
]
I Used Threshold To Get Pieces But The Is The Best Indicator That The Square Has A Piece In It.
Script
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
BOARD = 430
DIMENSION = 8 # dimensions of chess is 8*8
SQ_size = BOARD // DIMENSION
START_X = 40
START_Y = 33
DARK_SQUARE_THRESHOLD = 150
for row in range(8):
for col in range(8):
piece_image = img_gray[y:y + SQ_size, x: x + SQ_size]
th3 = cv2.adaptiveThreshold(piece_image,255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
image = cv2.rectangle(img, (x, y), (x + SQ_size, y + SQ_size), (0, 0, 0), 2)
x += SQ_size
x = START_X
y += SQ_size
This Is The Result Image:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|