Difference between revisions of "PlayChessWithAWebCam/BoardFinder"
(→Color filtering the background)
|(One intermediate revision by the same user not shown)|
|Line 91:||Line 91:|
Revision as of 06:16, 3 December 2019
- 1 Source Code
- 2 Algorithm
- 2.1 Finding the chessboard
- 2.1.1 Initializing the BoardFinder
- 2.1.2 Finding the chessboard corners
- 2.1.3 Expanding the found corners
- 2.1.4 Examples
- 2.1 Finding the chessboard
Finding the chessboard
Still it is very helpful and used as a basis for the BoardFinder.
Initializing the BoardFinder
The board finder is initialized with an image. Optionally you can specify the Video instance to be used.
finder = BoardFinder(image,video=video)
Finding the chessboard corners
The find method starts the board finding. The Boardfinder will try out different patterns from 7x7 down to 3x3. In fact we only need to check: 7x7, 7x5, 5x5, 5x3, and 3x3 for want of an even number of squares to be detected. We do not try 9x9 which would give us the full 8x8 board since it's very unlikely that our real chessboard will fulfill the requirements of the OpenCV algorithm mentioned in it's footnote: "
Note The function requires white space (like a square-thick border, the wider the better) around the board to make the detection more robust in various environments. Otherwise, if there is no border and the background is dark, the outer black squares cannot be segmented properly and so the square grouping and ordering algorithm fails. "
Since a successfull find is much quicker than an unsuccessful one (a factor of some 6 to 40 times) and since searching a small image is much faster than a big one the width of the searchImages can be specified. The default is 640 pixels. For the 13 tests images the search is even successful when the searchWidth is only 360 pixels. The difference in speed is approximately a factor of 1.5. Searching 13 test images and creating all the debug images takes around 8 seconds.
Expanding the found corners
Since we have used the OpenCV findChessBoard Corners not for it's intended purpose of camera calibration but for the detection of a real chessboard we have to adapt the result. The result of OpenCV's algorithm already gives us some hints e.g. whether the board is empty or is filled with pieces for the chess game's starting position.
Found chesspoint order issue
looks like some sorting is necessary see e.g.
looks like sorting by multiple columns is awkward in numpy see
Chessboard008 in detail
The corners detected can be used to fill the 4 corner polygons. We don't know the colors of the squares yet but we know that a chessboard-pattern was found.
See https://codegolf.stackexchange.com/questions/63772/determine-the-color-of-a-chess-square for some fun code around the issue to determine whether a square is light or dark from its position.
To find out the colors we mask the detected squares.
From now on we know which color is white/light and which one is black/dark.
Now we can filter the original image with the color ranges found in the mask / histogram steps.