23
$\begingroup$

I am making a robot that plays Tic Tac Toe

enter image description here

Currently, I have code that will parse the board and separate it into an array of 9 images, each space on the board. How can I detect if the image contains an X or and O? Also, I am running Mathematica 10

enter image description here

edit: This question is different from the proposed duplicate because the duplicate did not solve my problem and none of the solutions from it worked reliably with mathematica 10 for my purposes

$\endgroup$
2
36
$\begingroup$

Use the Classify[] function to train your own classification function (name it c) on a list of example photos of X's and O's (see reference pages on handwritten digits classification and the particular section in Classify)

enter image description here

Note that Classify is special algorithm that programs itself (it uses an artificial intelligence pattern recognition algorithm to "learn" which photo goes in which category). All you have to do is give it many different examples of X's and 0's and empty squares, point them to their "names" using -> and run it. You will need to make different images of the three categories X , 0 and emptySquare (perhaps 300 or so) to get Classify to generalise nicely and make a decent c function. (When I say "generalise" I mean "recognise the 3 patterns in new photographs".)

Here is my own attempt:

enter image description here

$\endgroup$
19
$\begingroup$

In this case (distinguishing x's from o's), it is also pretty easy to design your own classifier. Begin by binarizing, and then calculate the EulerNumber for each. The Eulernumber for x's will be zero, while the o's will have a component with Eulernumber 1 (The Euler number is the number of encircled regions). The erosion will help to make it a little more general, for instance, in case the circle isn't completely closed.

 ex1=Import["http://i.stack.imgur.com/4vK05.png"];
 ex2=Import["http://i.stack.imgur.com/zzw53.png"];
 ex3=Import["http://i.stack.imgur.com/28gkl.png"];
 ex4=Import["http://i.stack.imgur.com/r8CJn.png"];
 x = {ex1, ex2, ex3}

enter image description here

xb = Erosion[Binarize[#], 3] & /@ x;
ComponentMeasurements[#, "EulerNumber"] & /@ xb

{{1 -> 0}, {1 -> 0}, {1 -> 0, 2 -> 1}, {1 -> 0, 2 -> 1}}
$\endgroup$
4
$\begingroup$

training set

trainingSet = {
Import["http://i.stack.imgur.com/oJnVl.png"] -> x,
Import["http://i.stack.imgur.com/yrNij.png"] -> x,
Import["http://i.stack.imgur.com/pINOj.png"] -> x,
Import["http://i.stack.imgur.com/B1OUt.png"] -> x,
Import["http://i.stack.imgur.com/4g9x3.png"] -> x,
Import["http://i.stack.imgur.com/7rWxi.png"] -> x,
Import["http://i.stack.imgur.com/6yAA6.png"] -> x,
Import["http://i.stack.imgur.com/KNXYj.png"] -> x,
Import["http://i.stack.imgur.com/LF3aG.png"] -> x,
Import["http://i.stack.imgur.com/7HWtq.png"] -> x,
Import["http://i.stack.imgur.com/lxb1s.png"] -> x,

Import["http://i.stack.imgur.com/MHZS0.png"] -> blank,
Import["http://i.stack.imgur.com/poatG.png"] -> blank,
Import["http://i.stack.imgur.com/rPinx.png"] -> blank,
Import["http://i.stack.imgur.com/Dghwy.png"] -> blank,
Import["http://i.stack.imgur.com/k9Z2B.png"] -> blank,
Import["http://i.stack.imgur.com/aLTvV.png"] -> blank,
Import["http://i.stack.imgur.com/iAgyV.png"] -> blank,
Import["http://i.stack.imgur.com/ARq5z.png"] -> blank,
Import["http://i.stack.imgur.com/fzSjC.png"] -> blank,
Import["http://i.stack.imgur.com/sgcNF.png"] -> blank,

Import["http://i.stack.imgur.com/oFADs.png"] -> o,
Import["http://i.stack.imgur.com/cvphN.png"] -> o,
Import["http://i.stack.imgur.com/hTdzn.png"] -> o,
Import["http://i.stack.imgur.com/fNcLf.png"] -> o,
Import["http://i.stack.imgur.com/4Vzwu.png"] -> o,
Import["http://i.stack.imgur.com/TKCpt.png"] -> o,
Import["http://i.stack.imgur.com/ETAot.png"] -> o,
Import["http://i.stack.imgur.com/7kWOR.png"] -> o,
Import["http://i.stack.imgur.com/O8AaU.png"] -> o,
Import["http://i.stack.imgur.com/ZvfQe.png"] -> o}

newlyTrainedClassifyingFunction = Classify[trainingSet, Method -> "NeuralNetwork"]

testSet = { (* new data from a tic tac toe game*)
Import["http://i.stack.imgur.com/5n92Y.png"],
Import["http://i.stack.imgur.com/2ieav.png"],
Import["http://i.stack.imgur.com/HGjpN.png"],
Import["http://i.stack.imgur.com/e8JRg.png"],
Import["http://i.stack.imgur.com/sBh59.png"],
Import["http://i.stack.imgur.com/JB9hM.png"],
Import["http://i.stack.imgur.com/8ewT3.png"],
Import["http://i.stack.imgur.com/4hz6m.png"],
Import["http://i.stack.imgur.com/Pfezq.png"]}

newlyTrainedClassifyingFunction[testSet]

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.