I'm trying to detect the grid in sudoku puzzles using OpenCV but I'm having troubles with the last steps (I guess).
What I'm doing is:
- Downsaple the image
- Blur it
- Applying a highpass filter (bilateral)
- Thresholding the image, using adaptive threshold
- Some dilations and erosions
All this gives me the following images:
From now on, I need to detect the grid, and I found a few methods of how to do that but none of them gave me the confidence of being robust enough.
The first one is to find lines using Hough transform but I find a lot of spurious lines.
The other is using connected components, which gives me the best results. I tried to implement RANSAC as a way to get the right centroids, but I'm not having good results and also takes a while to get the answer ("a while" is less than 2 seconds, but later I want to use it in real time video).
Any idea how this can be done? I mean, how can I discard the wrong centroids and start solving the sudoku?
Hough transform is definitely the way to go. In fact grid detection is one of the most popular example when introducing this tehcnique (see here and here).
I suggest the following steps:
At the last step you have many possible ways to go and it strongly depends on what you want to do with the results afterwards. For example you could create a new edge image with the found images and apply erosion and hough again, you could use something Fourier-based, or you could just simply filter the lines by some arbitrary threshold values (just to mention a few). I implemented the last one (since conceptually that is the easiest one to do), here is what i did (although i am not at all sure whether this is the best approach or not):
See code, have fun: