chess-diagram: improve board edge detection and add style for 5334 problems (Polgar) - 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 6a468b63e4b0d34e85596acc7561021921d12f63
(DIR) parent 9991fa9ec94dff59af9449af23e91edb72ce1aa0
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 28 Sep 2025 17:26:00 +0200
chess-diagram: improve board edge detection and add style for 5334 problems (Polgar)
https://www.amazon.com/Chess-5334-Problems-Combinations-Games/dp/1579125549
Diffstat:
A opencv/chess-diagram/board_styles/… | 0
M opencv/chess-diagram/main.py | 22 +++++++++++++++-------
2 files changed, 15 insertions(+), 7 deletions(-)
---
(DIR) diff --git a/opencv/chess-diagram/board_styles/5334_polgar.png b/opencv/chess-diagram/board_styles/5334_polgar.png
Binary files differ.
(DIR) diff --git a/opencv/chess-diagram/main.py b/opencv/chess-diagram/main.py
@@ -15,7 +15,7 @@ config = {
"debug": True,
"debugimage": True,
"checkmoveindicator": True,
- "boardstyle": "lichess", # "lichess", "woodpecker"
+ "boardstyle": "", # "5334_polgar", "lichess", "woodpecker"
"guessstyle": True,
"flip": False, # scanned board is flipped?
"flipdisplay": False # output should be flipped?
@@ -365,14 +365,20 @@ def detectpieces(boardimage, boardstyle, onlykings, flip):
return boardstate
def boarddetect(img):
+ areas = []
gimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
+ # dilate/erode: improves detection with broken lines.
+ kernel = np.ones((3,3), np.uint8)
+ d_im = cv2.dilate(gimg, kernel, iterations=1)
+ e_im = cv2.erode(d_im, kernel, iterations=1)
+
# canny edge (without gaussian blur)
- canny = cv2.Canny(img, 100, 100)
- contours, hierarchy = cv2.findContours(canny,
+ cimg = cv2.Canny(e_im, 100, 200)
+
+ contours, hierarchy = cv2.findContours(cimg,
cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # simple only stores points
- areas = []
for c in contours:
x, y, w, h = cv2.boundingRect(c)
# skip too small areas (for example 8 * piece size)
@@ -470,6 +476,9 @@ if len(areas) == 0:
debug("unique areas: %d" % len(areas))
+if config["debug"] or config["debugimage"]:
+ debugimg = fullimg
+
flip = "flip" in config and config["flip"]
dareas = []
boardstates = []
@@ -523,7 +532,7 @@ for area in areas:
continue
# check move indicator: check for any figure next to the region (about one square).
- if boardstyle == "woodpecker" and config["checkmoveindicator"]:
+ if boardstyle in ["5334_polgar", "woodpecker"] and config["checkmoveindicator"]:
# top right: black to move
mx1 = x2 + 4
mx2 = int(x2 + (area["w"] / 8))
@@ -571,7 +580,6 @@ for area in areas:
# draw a debug image with detected board regions and pieces.
if config["debug"] or config["debugimage"]:
- debugimg = fullimg
for b in boardstates:
if "area" in b:
area = b["area"]
@@ -613,7 +621,7 @@ if config["debug"] or config["debugimage"]:
fontscale = 1.0
cv2.putText(debugimg, c, (px, py + int(sh)), cv2.FONT_HERSHEY_SIMPLEX, fontscale, color, 2, cv2.LINE_AA)
- if boardstyle == "woodpecker" and config["checkmoveindicator"]:
+ if boardstyle in ["5334_polgar", "woodpecker"] and config["checkmoveindicator"]:
# side to move indicator.
if "side" in b and b["side"] == "w":
mx1 = x2 + 4