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

3 Answers 3

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["https://i.sstatic.net/4vK05.png"];
 ex2=Import["https://i.sstatic.net/zzw53.png"];
 ex3=Import["https://i.sstatic.net/28gkl.png"];
 ex4=Import["https://i.sstatic.net/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["https://i.sstatic.net/oJnVl.png"] -> x,
Import["https://i.sstatic.net/yrNij.png"] -> x,
Import["https://i.sstatic.net/pINOj.png"] -> x,
Import["https://i.sstatic.net/B1OUt.png"] -> x,
Import["https://i.sstatic.net/4g9x3.png"] -> x,
Import["https://i.sstatic.net/7rWxi.png"] -> x,
Import["https://i.sstatic.net/6yAA6.png"] -> x,
Import["https://i.sstatic.net/KNXYj.png"] -> x,
Import["https://i.sstatic.net/LF3aG.png"] -> x,
Import["https://i.sstatic.net/7HWtq.png"] -> x,
Import["https://i.sstatic.net/lxb1s.png"] -> x,

Import["https://i.sstatic.net/MHZS0.png"] -> blank,
Import["https://i.sstatic.net/poatG.png"] -> blank,
Import["https://i.sstatic.net/rPinx.png"] -> blank,
Import["https://i.sstatic.net/Dghwy.png"] -> blank,
Import["https://i.sstatic.net/k9Z2B.png"] -> blank,
Import["https://i.sstatic.net/aLTvV.png"] -> blank,
Import["https://i.sstatic.net/iAgyV.png"] -> blank,
Import["https://i.sstatic.net/ARq5z.png"] -> blank,
Import["https://i.sstatic.net/fzSjC.png"] -> blank,
Import["https://i.sstatic.net/sgcNF.png"] -> blank,

Import["https://i.sstatic.net/oFADs.png"] -> o,
Import["https://i.sstatic.net/cvphN.png"] -> o,
Import["https://i.sstatic.net/hTdzn.png"] -> o,
Import["https://i.sstatic.net/fNcLf.png"] -> o,
Import["https://i.sstatic.net/4Vzwu.png"] -> o,
Import["https://i.sstatic.net/TKCpt.png"] -> o,
Import["https://i.sstatic.net/ETAot.png"] -> o,
Import["https://i.sstatic.net/7kWOR.png"] -> o,
Import["https://i.sstatic.net/O8AaU.png"] -> o,
Import["https://i.sstatic.net/ZvfQe.png"] -> o}

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

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

newlyTrainedClassifyingFunction[testSet]

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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