Can someone give me a pointer on how to solve this scenario:
1) I have an image of a chessboard (8x8) with black and white squares. I need to analyze the image and create a 8x8 matrix corresponding to the chessboard.
2) Store 0 if the corresponding chessboard cell is white, store 1 if the corresponding chessboard is black

The chessboard images have constant size, color depth and resolution. The image might be rotated.

Dani AI

Generated

A compact, robust approach that handles rotated scanned boards is: locate the board region, rectify its perspective to a square, split it into an 8x8 grid, then classify each cell by a robust brightness measure. This works reliably on printed puzzles (like the scanned journals mentioned by ) and avoids per-pixel brute force checks while tolerating rotation and minor scanning artifacts.

Practical pipeline (high level)

  • Read image and convert to grayscale. Use smoothing to reduce scanner noise.
  • Find the largest quadrilateral that looks like the board (edge detector + contour approximation, or Hough lines when contours fail). This follows the edge-oriented idea from but gives concrete steps to get a 4-point polygon.
  • Apply a perspective transform to warp the board to a known square size (for stable sampling).
  • Divide the warped image into an 8x8 grid. For each cell, compute the median (or mean after discarding extremes) of a small subpatch away from the geometric center to reduce bias from pieces printed on the squares.
  • Use a global threshold (Otsu) or per-cell adaptive threshold to decide white (0) vs black (1). If orientation matters (e.g., “white on right”), rotate the matrix until that convention is satisfied.

Short Python/OpenCV sketch

import cv2, numpy as np

img = cv2.imread('board.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
# Canny, findContours, approxPolyDP -> get 4 pts `pts`
M = cv2.getPerspectiveTransform(np.float32(pts), np.float32(dst_square))
warped = cv2.warpPerspective(gray, M, (800,800))
cell = warped[y:y+cs, x:x+cs]  # sample subpatches per cell, use median, threshold

Troubleshooting notes

  • If contour detection fails (partial crop, heavy noise), try Hough lines or expand thresholds.
  • For heavy piece occlusion, mask large connected components (the pieces) and base square color on remaining pixels or use multiple small samples per cell and take the median.
  • If lighting is uneven, use adaptive thresholding or local contrast normalization before sampling.

This gives a deterministic 8x8 binary matrix suitable for digitizing many scanned puzzles. ’s format question is relevant only for how you load images; OpenCV/Pillow handle PNG/JPG/BMP without changing the algorithm above.

Recommended Answers

All 5 Replies

What kind of image? PNG? Bitmap? Some other format?

Png or bitmap or jpeg. I know there is a brute force approach to check for rgb values of every pixel, but is there anything else?

edge detection? Or find first black pixels and see if it is corner, use it to choose between alternatives starting with 1 or starting with 0.

I think you do not give full info, for example there must be some background also.

edge detection? Or find first black pixels and see if it is corner, use it to choose between alternatives starting with 1 or starting with 0.

I think you do not give full info, for example there must be some background also.

I want to digitize hundreds of chess puzzles from Russian hobby journals. The puzzles are scanned into PDF. I extracted images and now want to convert chessboards into arrays

could you not post what you have and atttach few of the pictures from advanced editor -> manage attachments. Sounds interesting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.