Wednesday 27 May 2015

GitHub.

I setup a GitHub account yesterday.
At first, you might find it difficult to understand but it is a piece of cake actually.

GitHub is an online platform for sharing open-source projects and software developments. Basic motto of it is to keep everything in sync in a team. You can invite someone to contribute to your code, you can check on-going various projects, contribute to them and push it on for responses.

You'd need a GitHub account, GitHub installed on your computer and know 5 to 6 commands. There comes a GUI as well as a Shell application of GitHub on your computer. I'd prefer command line one.

Follow the link below for learning basics of GitHub. Great tutorials. (Y)
GitHub tutorial by LearnCode.academy

Backtracking algorithm is a great tool.

So I wanted to solve the SUDOKU I just recognized from the image I fed in MATLAB and I tried various logic but nothing worked better than "Backtracking". You can find a great explanation of this on YouTube or  http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/  .
Basically, a function is made to recur and return values as true or false if the consistency is maintained or not respectively.

Unlike N-Queen problem where you can output simultaneously, you might find need a global variable that stores the ANSWER matrix of SUDOKU if no empty boxes are possible; or else returning values and stepping out of recurring functions might result in losing of the solved matrix.
I could solve a 9x9 SUDOKU in 3 seconds. My program needs optimization.

Will keep posted on program optimization and better image processing techniques because the current one carries several cons.

Tuesday 26 May 2015

Getting better OCR results from mazes or puzzles - Method 1.

ocr() function in MATLAB supports few helpful attributes.
Following three(roi , TextLayout and CharacterSet) might help you increase OCR efficiency in reading characters from puzzles or places where you may have an idea of spacing between characters.

Step 1: Set up a grid of Region Of Interest(roi).
I am working on a Sudoku puzzle solving program. So basically, I know the size of Sudoku I am feeding my program. This gives me a rough idea of each small cell dimensions. Or you can find it a more non-rigid way by :
1.Find size of image processed and cropped maze.
2.Divide rows and columns as Sudoku dimensions.

"Moreover, your region of interest is not the complete cell Of course, the interested character lies in middle of the cell. So amend the rectangle dimension as necessary and set up an array of xmin, ymin, width and height of rectangle."

[row column]=size(out); %out is my image-processed array
r_round=round((row)/9);
c_round=round((column)/9);
u=1;
for i=0:8
    for j=0:8
        xinit= i*c_round+20;          % +20 and -35 is for adjusting coordinate a bit from theoretical
         yinit= j*r_round+35;       %value as  we are interested in central part of cell where element lies.
         roi(u,1:4)=[xinit yinit 125 125];
        u=u+1;
    end
end

Step 2: This step is probably the final step. It is kind of simple. Just direct the in-built ocr() function to do the job and provide parameters correctly.

ocrtext=ocr(out,roi,'TextLayout','Block','CharacterSet','123456789');

%Here 'TextLayout' attribute specifies the layout of characters to be read. I have set it to 'Block' as %my characters won't have a continuity in background and moreover, they are like blocks. You can %check Documentation of MATLAB for other values. 'CharacterSet' specifies  which characters %shall be considered for matching the values. I have put 1 through 9 as my Sudoku puzzle has only %digits.

----------------------------------------------------------------------------------------------------------------------
ocrtext cell contains ocrWords, ocrConfidences and various useful props.
The ocrtext will contain 81 ocrtext cells. Each corresponding to a cell on SUDOKU maze.
Store these cells in an array and use it for further SUDOKU solving.
Be careful about cells and arrays. Know difference between them and convert cells to matrices using cell2mat().
Refer Documentation of MATLAB  by typing "doc"(without quotes) in command window.

Friday 22 May 2015

Glitch in the "Bounding Box" method.

Regarding the last post. The method I posted had a major drawback.
I will explain with the help of an example.

I tried to feed my code a real sudoku problem. And it so happened that my OCR results storing array had a number "95126".

This was because it considered the less spaced numbers as a single number and hence stored in a single cell. This was a havoc for me; but I found an alternative. I will post that soon,
Keep smiling. :)

Found a solution to storing characters recognized from MATLAB in an array in a program to solve Sudoku.

The key were bounding boxes.
I found out the elements recognized having same bounding box row coordinates and hence stored their column coordinates in same row of an array and changed row of array when it didn't meet my condition of same row location.
This way I got an array of elements of my sudoku puzzle sorted row-wise.

Here's the trick for storing it column-wise.
I made a scale of pixels showing distance between a block and first block. You can find out prcatically using impixelinfo how many pixel difference corresponds to number of blocks.
Like in my case 150 pixels corresponded to center to center of two adjacent boxes. Hence I named a variable to store "what-multiple-of-150" is the non-empty character containing pixel.

This way you can sort it column-wise and hence get a complete Sudoku puzzle from page to matrix using basic knowledge of MATLAB.

P.S. This method has major drawbacks. I will post the alternative I found soon. Thank you.  

Wednesday 20 May 2015

Stuck. Need help.

I am making a sudoku solver using MATLAB. So far I completed cropping my sudoku puzzle from image and even extracting numbers accurately. But the problem is the numbers are extracted in text file of OCR regardless of position of where they are. Need help to store my recognised characters in a matrix to further process for solution.

Tuesday 12 May 2015

Summer.

So here it is. Summer vacations. Time to set aims clear and start Image Processing and look forward to Advanced AVR. Heads up.