# Sudoku Solver

2,574

14

6

## Introduction: Sudoku Solver

Take a picture of a Sudoku and let python solve it for you!

When you try to solve your Sudoku puzzle and feels hard to find its solution or even you are not sure for the correct answer. All you need to do is to click a picture of Sudoku and wait for a few seconds, Sudoku Solver will solve the puzzle for you automatically.

My class guides you through the how to setup, run and how it is working so that you can Solve Sudoku with an Image. This class will require some basic python knowledge as you have to deal with some code but even if you don’t have knowledge of python, there is still explanation provided.

I hope you will join me in this Lazy Little Project.

## Step 1: Setting Up Python and OpenCV (Prerequisites):

1) Python 2.7 but not Python 3

2) Numpy (1.11.2)

Open terminal/ Command Prompt and type below command

`pip install numpy`

3) OpenCV

For Ubuntu, Open terminal and type below command

`sudo apt-get install python-opencv `

For Windows Install OpenCV from - http://docs.opencv.org/3.2.0/d5/de5/tutorial_py_se...

OR

by following video https://www.youtube.com/watch?v=3xAslL8htuQ

4) Python Image Library (4.2.1)

Open terminal/ Command Prompt and type below commandp

`pip install pillow `
but wait, 1 thing still left!!!

Before you can start using Python in Windows (No need if Ubuntu), you need to add the python interpreter to the set of Environmental Variables:

1. In Windows 8/8.1, go to the right corner of your screen and click on Settings.

2. Then select PC Info.

3. In Windows 10, go to File Explorer. Right click on This PC, and select Properties. (This also works for Windows 7/8/8.1)

4. Click on Advanced System Settings.

5. Next, click on Environment Variables.

6. In System Variables, find `Path` variable. Select it and click on Edit.

7. In the end of the list add C:\Python27 and C:\Python27\Scripts (Adding Python27\Scripts is not mandatory though ).

8. Click OK.

Congratulations, now you can use Python from cmd and Windows Powershell as well.

## Step 2: How to Run/use:

Now that you've set up OpenCV and Python, we are ready to do some Image Processing.

2. Save it in a folder on your PC

3. Locate to that folder

4. Save an image of Sudoku you want to solve with name sudoku.png

5. On Ubuntu: Right-click anywhere on that folder and click open terminal

`python suduko_solver.py`

5. On Windows: Just double click suduko_solver.py

Congratulations!! Your browser should pop showing solved Sudoku from just that image.

## Step 3: Working:

Here's a Sudoku image and here's the solution

## Step 4: Algorithm:

1. Basic image preprocessing - Thresholding.

2. Finding black lines – Hough Transform

3. Get rectangle made by lines

3.1. Finding the edges

3.2. Crop rectangle

3.3. Resizing to perfect square

4. We will extract cells from this, by slicing the sudoku grid evenly.

5. Recognizing digits from each slice

5.1. Each digit recognized get saved in data.txt

5.2. If slice is empty it will be counted as 0

6. Solving Sudoku using a recursion.

7. Output solution in HTML table.

## Step 5: Here Are Some Illustrations of the Different Stages:

After Preprocessing:

Final processed Sudoku Grid:

Here are a few digits after slicing:

Solved Grid:

## Step 6: Contact Me:

If you have any queries or you ran into some problems or you

have some suggestions, feel free to contact me.

My Name is Nikhil Taneja and my email id is: -

taneja.nikhil03@gmail.com

Fork me on GitHub: - My Profile is https://github.com/ItsNikhil

Have a Nice Day!!!!!!!

Participated in the
Automation Contest 2017

Participated in the
Lazy Life Challenge

108 3.6K
52 5.7K
1 127 12K
8 149 24K

## 6 Discussions

ye i did it

Great tutorial. I know a lot of people who could really state to read this.

Glad to hear this <3
Please vote for me