add opencv experiment, detect chess pieces on a board image - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit f965ad5fc39d27bacf04cdf43a223e5c2c283a02
(DIR) parent 060a24b2ff394635468c107250cef60c45322f17
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 6 Jan 2024 13:23:36 +0100
add opencv experiment, detect chess pieces on a board image
Diffstat:
A opencv/chess-detect/board.png | 0
A opencv/chess-detect/piece.png | 0
A opencv/chess-detect/piece_mask.png | 0
A opencv/chess-detect/test.py | 52 +++++++++++++++++++++++++++++++
4 files changed, 52 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/opencv/chess-detect/board.png b/opencv/chess-detect/board.png
Binary files differ.
(DIR) diff --git a/opencv/chess-detect/piece.png b/opencv/chess-detect/piece.png
Binary files differ.
(DIR) diff --git a/opencv/chess-detect/piece_mask.png b/opencv/chess-detect/piece_mask.png
Binary files differ.
(DIR) diff --git a/opencv/chess-detect/test.py b/opencv/chess-detect/test.py
@@ -0,0 +1,52 @@
+# experiment for chess: simple detecting of pieces from board image.
+# testing different match templates.
+# it doesn't do any scaling/transforms so the board size and image to detect
+# are unscaled etc.
+
+import cv2
+import numpy as np
+
+# Load the chess board and chess piece images
+img_board = cv2.imread('board.png')
+img_piece = cv2.imread('piece.png')
+
+#img_piece_mask_bg_img = cv2.imread('piece_mask.png')
+#img_piece_mask_bg = cv2.cvtColor(img_piece_mask_bg_img, cv2.COLOR_BGR2GRAY)
+
+img_piece_mask = cv2.cvtColor(img_piece, cv2.COLOR_BGR2GRAY)
+
+# Convert both images to grayscale
+img_board_gray = cv2.cvtColor(img_board, cv2.COLOR_BGR2GRAY)
+
+# Apply morphological operations to extract the chess piece from the board
+kernel = np.ones((3, 3), np.uint8)
+img_piece_mask = cv2.erode(img_piece_mask, kernel, iterations=1)
+img_piece_mask = cv2.dilate(img_piece_mask, kernel, iterations=1)
+
+# https://docs.opencv.org/3.4/d4/dc6/tutorial_py_template_matching.html
+
+w, h = img_piece_mask.shape[::-1]
+
+#ret, mask = cv2.threshold(img_piece_mask, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
+#cv2.imwrite('/tmp/mask.png', mask) # DEBUG
+#cv2.imwrite('/tmp/piecemask.png', img_piece_mask) # DEBUG
+
+# Find the matching location on the board
+#res = cv2.matchTemplate(img_board_gray, img_piece_mask, method=cv2.TM_SQDIFF, mask=mask)
+#threshold = 8
+
+res = cv2.matchTemplate(img_board_gray, img_piece_mask, method=cv2.TM_CCOEFF_NORMED, mask=img_piece_mask)
+threshold = 0.8
+
+#res = cv2.matchTemplate(img_board_gray, img_piece_mask, method=cv2.TM_CCORR_NORMED, mask=mask)
+#threshold = 0.8
+
+loc = np.where(res >= threshold)
+#loc = np.where(res < threshold) # cv2.TM_SQDIFF
+for pt in zip(*loc[::-1]):
+ cv2.rectangle(img_board, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
+
+# Show the result
+cv2.imshow('Result', img_board)
+cv2.waitKey(0)
+cv2.destroyAllWindows()