This is the C++ implementation of the dynamic chess board piece extraction system. It uses YOLOv5 for piece detection, and chessboard and square extraction is identical to the Python implementation.
This is the first phase of the C++ version, so I will update this repository.
For now, only the perspective transform method is implemented, and the model is only for demo purposes. A better model will be trained in the near future.
In C++, there are not many libraries like in Python. That is why the chessboard is prepared directly, and additional chess piece images are used (chess-pieces folder).
- OpenCV 4.x
- CMake 3.10 or higher
- C++17 compatible compiler
src/- Source code filesinclude/- Header filesmodels/- YOLOv5 model filestest-images/- Sample images for testingextracted-data/- Output results and CSV fileschess-pieces/- Individual piece imagesbuild/- Build directory (generated).vscode/- VS Code configuration
main.cpp- Main application entry pointconfig.cpp- Configuration settings and pathsimage_processing.cpp- Basic image processing operationsfind_contours.cpp- Contour detection algorithmsfind_biggest_contour.cpp- Largest contour identificationfind_valid_squares.cpp- Chess square validationperspective_transform.cpp- Perspective transformation operationsinverse_transform.cpp- Inverse transformation operationssave_squares_to_csv.cpp- CSV export functionalitydisplay_squares_from_csv.cpp- Visualization from CSV datachessboard_analyzer.cpp- Chess board analysis logicfen_board_generator.cpp- FEN notation generation
Each source file has a corresponding header file in the include/ directory.
chess-yolov5m.onnx- YOLOv5 model in ONNX formatchess-yolov5m.pt- YOLOv5 PyTorch modelclasses.txt- Chess piece class labels
mkdir build
cd build
cmake ..
cmake --build .
./Debug/main.exeThe project uses CMake for cross-platform building. The CMakeLists.txt file:
- Sets C++ standard requirements
- Links OpenCV libraries
- Includes header directories
- Compiles all source files into the main executable
- Place your chess board image in the
test-images/folder - Update the image path in
config.cppif needed - Run the compiled executable
- Results will be saved in the
extracted-data/folder
The program generates:
- Processed chess board images
- CSV files with square coordinates
- FEN notation of the board position
- Individual piece images