Saturday 20 June 2015

A college assignment - Tic Tac Toe using simple A.I. algorithms.

I got a call from my friend asking me to help with his college assignment on C programming. It had a 3x3 Sudoku solver on list which I had worked earlier here using MATLAB; so I picked up a tic tac toe game, both multiplayer and Computer opponent.

I didn't use Google this time and came up with a fully home-made logic recipe which you can find below :

Here is the formal initialization parts and I declared a function called printer() which prints my tic tac toe game board along with 'x' and 'o' pieces every time called.



So this is an another function called  win() which is a pretty long one; long but dumb. I just ran three to four loops to check horizontally, vertically and both diagonally for a three same 'x' or 'o's and return a flag if found one, implying the end of game. The final case is a no space on game board and all place filled with 'x' or 'o' declaring a draw. It also prints the winners name. (Don't worry I will add a link to the complete code if you want to look through it)


This is my main() function. An infinite loop runs till 'e' character is pressed or a win or draw flag is raised. In this infinite loop, an input is taken from the player whenever the chanceflag is 0 implying that it is Player's chance. 'w','a','s' and 'd' keys are used to print a "_" on the game board to show cursor position. When a ' '(space) is hit, the program checks for that place to be empty and if empty then the user is allowed to input a character. The character is permanently placed there if it is 'x'. The chanceflag is set 1 once input is confirmed.

Next comes the A.I. part.

The c_row and c_col variables contain position of last input from player. The Computer performs a check for two 'x' on the same c_row,c_col and cases where the place is on diagonal; c_row=c_col(main diagonal) or c_row=2-c_col(anti diagonal). If 2 'x' are found then the priority of program is to fill the empty space with an 'o'; otherwise it fills the space with a randomly picked value of row and column using rand()/15000(Because maximum value of rand() is 32767, i.e. if divided by 15000 quotient is between 0 and 2). Labels have served a great purpose in program, check them for sure.

You can find the complete code here : Tic Tac Toe using simple A.I.

Do comment a better approach to this program! Thank you!

Friday 12 June 2015

Object Tracking using MATLAB.

This might sound like a tough one but believe not that much of a rocket science.

I wore a red cloth on my finger and took a video of me moving the finger.(Still don't have webcam hardware support; do share me a link to download this externally, not from MATLAB site because my software is cracked). This video was fed in MATLAB by following the examples in documetation of how to take in video inputs using VideoReader() function. It makes a video object from which frames can be extracted in a structure in the following way,

















So what we do here is create a structure(which is analogous to C structures) which contains frames in fields called 'cdata' while its colormap in 'colormap'. I am working with a RGB image so it doesn't need colormap. A loop is run until no frames are left in the object and structure stores frames in it during every iteration.

This makes our frames ready to be image processed. I took out the R, G and B frame separately from every frame(RGB images are formed by stacking three layers of Red, Green and Blue images). But just picking high Red value from a frame doesn't define it Red. Say for example, white has RGB components (255,255,255); so all my white colors will also be segmented, which I don't want.

A red component means high R, low G and low B component. Use impixelinfo on a frame to see the practical values.

imshow(s(150).cdata); impixelinfo;

Keeping the values in mind, threshold all three frames and then add them up to show only red regions of frame.


Now it's time to filter the minor noises using connected component analysis as did in Sudoku Solver project and dilate the remaining red component to form a good shape. Run all this in a for loop till the last structure element arrives and keep outputting image using imshow() and pause(0.01) functions. Pausing is necessary to see the video.

Now all you have to do is take the mean of all white pixels and show shape on its gray-scale image.
Happy object tracking!
The following video is a demonstration of the object-tracking. The yellow spot on finger follows the finger in x and y both, while the bar on left takes only y components. I aim to use this to play a ping-pong game.

Where should you start to image-process?

If you are interested in image-processing, download MATLAB from torrents. Probably it is the only place you can get it for free. But the problem is you don't get hardware support packages cracked. So if you want to start anew, start with OpenCV. I will be soon moving to OpenCV because it is open-source.
For tutorials, you can go through,
1. https://www.youtube.com/playlist…
The above playlist covers only very simple basics that will comfort you with the environment. The key is MATLAB documentation. Open up an example go through it and search for every function, read its syntax and what it does.
The problem with this project is it is not very robust. It will occasionally pick noises or wipe out a number from Sudoku image. My image processing techniques work but they ain't fool-proof. To counter that I have provided a manual correction method within the code.

You scroll down the blog what problems were faced earlier, what alternate solutions I came with, why I discarded some etc.

Right now, I am working on a Finger controlled Ping Pong game, integrating C and MATLAB. Till now I am successful to object track and make a ping-pong game in C. Will soon integrate them and blog. Feel free to share your views. :)

Friday 5 June 2015

Finalized my Sudoku Solver project.

So finally, I was able to solve a few Sudoku problems from newspapers using my program. The Sudoku solving algorithm seems fool-proof till now yet image-processing not satisfactory to me.
You may look out for my project in the CustomizationOfGrid branch of following link to GitHub here. Don't forget to view README.md.

Following are raw ideas of my image-processing script :
1. Cropping the image by finding the corners of the cleared border image.
2. Straightening by finding mean corners.
3. Perform Contrast enhancement, Connected components analysis and filter.
4. Setting up ROIs for each Sudoku cell and prompt menu to adjust ROI manually if found incorrectly placed w.r.t. the number in cell or surrounding cell.
5. Perform OCR.
6. For characters below certain Confidence levels print 'x' and the user must amend to it.

Don't forget to comment any better methods to image process such images of puzzles.