Building a simple SUDOKU Solver from scratch - Part 1: Grid Detection & Digit Extraction
Hi, today I’m gonna explain how to build a simple SUDOKU Solver taken from image step-by-step. You can see the demo of SUDOKU Solver in the GIF image below.
To build this program, we will go through 4 main steps.
1. SUDOKU Grid Detection and Digit Extraction
2. Recognize Number with Support Vector Machine (SVM)
3. Solving SUDOKU with Backtracking Algorithm
4. Completing the simple SUDOKU Solver
This program was built by Python 2.7 and OpenCV 2.4.13 so before you want to follow this post, please install Python 2.7 and OpenCV 2.4.13 Update: Since some people asked me to modify the code to work with OpenCV 3.x, please refer to this link if you’re using OpenCV 3.x SUDOKU_OpenCV3
1. SUDOKU Gird Detection and Digit Extraction
The most important thing to solve a SUDOKU board from images is detecting SUDOKU grid (or line). I found many ways to extract digit from SUDOKU on the internet, but I saw this article (http://jponttuset.cat/solving-sudokus-like-a-pro-1/) is the smartest and easy way to extract digit from SUDOKU board. I took the idea from that guy and re-implement myself with Python. Let’s get started!
We will use a famous and simple technique called Hough Line Transform to detect lines on the image. If you’re not familiar with Hough Line Transform, please check out my previous article about Hough Line Transform
Note: We can visit this website to print any SUDOKU images for practicing: http://show.websudoku.com/
From an RGB frame captured from the webcam, we will convert it to a grayscale image. After that, extract edges using Canny Edge detection and then apply Hough Line Transform to detect lines. We use cv2.HoughLines() with \(\rho\) unit equal to \(2\) and minimum length of line to be detected is \(300\) pixels. It means increasing the accumulator by \(2\) when a point is satisfied and consider to be a line when the minimum value in the accumulator is \(300\). The result will look like this:
Fig. 1. Line detection using Hough Line Transform
The next step is looking for intersections between these lines. When we know the intersection points, we can extract every block that contains SUDOKU digit.
Hmm … Look at that! There are too many lines. Synonymous with there are some redundant lines (some lines are overlapped each other).
But wait, we already know the distance of each line to the origin. It means we can easily remove the overlapped lines if the distance between these lines is close to each other.
Let’s say we will remove the redundant lines if the distance between these lines is less than \(10\). Firstly, let’s sort these line increasingly by the distance (\(\rho\)) and then check the distance of every \(2\) lines. If the distance is less than \(10\), remove the second line.
You can read the following code
Now, the result looks better than before.
Fig. 2. SUDOKU Grid after removing redundancy lines
Next, we will find the intersection points based on those lines. Just to recap again, every line is satisfied this equation: \(\rho=x\cos \theta+y\sin \theta\) (1)
We have \(\rho\) and \(\theta\) for each line, so it’s easy to find the intersection between 2 lines by solving linear equations. In this post, I use the \(linalg\) library in Numpy to find the intersection points. You can check out this link to see how to use \(linalg\) in Python: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html
Just change a bit of code and found the intersection points like this
Fig. 3. Intersection points detection
If we have those points, we also can extract the bouding boxes of the digit number in the SUDOKU board.
Fig. 4. Bounding block for each digit
Here is the code for digit number extraction
To make sure that we extract the correct digit blocks, I set the condition which when the number of intersection points is equal to 100, we start to extract the digits. You can check out the above code.
Bravo! Now we know how to extract digit number block from SUDOKU board. In the next article, we will recognize those digit numbers with Support Vector Machine algorithm :)