https://github.com/trflorian/sudoku-extraction
Extract Sudoku grids from images
https://github.com/trflorian/sudoku-extraction
computer-vision opencv-python python sudoku
Last synced: 9 days ago
JSON representation
Extract Sudoku grids from images
- Host: GitHub
- URL: https://github.com/trflorian/sudoku-extraction
- Owner: trflorian
- License: mit
- Created: 2025-09-27T21:36:24.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-10-16T10:24:07.000Z (9 months ago)
- Last Synced: 2025-10-28T15:22:58.372Z (9 months ago)
- Topics: computer-vision, opencv-python, python, sudoku
- Language: Python
- Homepage: https://towardsdatascience.com/classical-computer-vision-and-perspective-transformation-for-sudoku-extraction/
- Size: 707 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sudoku Grid Extraction


This project focuses on extracting and processing Sudoku grids from images using classical computer vision techniques.
It includes functionalities for detecting the grid, warping the image, and preparing it for further analysis or digit recognition.

## Quickstart
This project supports `uv` for easy environment management and script execution.
To run the extraction, you can use the provided script.
Some sample sudoku images are available in the `images/` directory.
```bash
uv run scripts/extraction.py --image images/sudoku_001.jpg
```


### Corner Sorting
The project includes a utility for sorting the corners of the detected Sudoku grid in a consistent order (top-left, top-right, bottom-right, bottom-left).
```bash
uv run scripts/visualize_corner_sorting.py --type simple
```
```bash
uv run scripts/visualize_corner_sorting.py --help
usage: visualize_corner_sorting.py [-h] [--type {cyclic,assignment,centroid,simple}] [--fps FPS] [--rot_speed ROT_SPEED]
```

## Processing Pipeline
The main processing steps include:
1. **Grayscale Conversion**: Convert the input image to grayscale.
2. **Canny Edge Detection**: Apply Canny edge detection to highlight the edges in the image.
3. **Contour Detection**: Find contours in the edge-detected image.
4. **Grid Detection**: Identify the largest quadrilateral contour, which is assumed to be the Sudoku grid.
5. **Corner Sorting**: Sort the corners of the detected grid using a specified method (e.g., cyclic sort with centroid anchor).
6. **Perspective Transformation**: Warp the image to obtain a top-down view of the Sudoku grid.
## References
- [OpenCV Documentation](https://docs.opencv.org/)
- [SciPy Linear Sum Assignment](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html)