9
$\begingroup$

I have a tic tac toe board I want to dynamically process in mathematica to have my robot play a user in tic tac toe. In order to determine the users move, I want to slice the board into 9 images, which are the 9 boxes of the board. How can I slice this image of the board into 9 boxes representing each space where there can be a move?

example board

The image has white space and therefore using imagePartition produces something like:

enter image description here

This is because the boxes are not perfect squares, and the board is not perfectly in the center of the image.

Additionally, here is an example of the board that our delta robot draws: enter image description here

$\endgroup$
7

2 Answers 2

5
$\begingroup$
Framed[i = Import["https://i.stack.imgur.com/tTeBU.png"]]

Mathematica graphics

ib = ColorNegate@Binarize@i;
sc = SelectComponents[ib, "Count", -1];
bb = ComponentMeasurements[sc, "BoundingBox"];
bs = Reverse[Sort /@ {#[[1]], Last@ImageDimensions@sc - #[[2]]} &@ Transpose[bb[[1, 2]]]];
it = ImageTake[sc, Sequence @@ bs];
it1 = ImageTake[i, Sequence @@ bs];
eps = MorphologicalTransform[Thinning@it, "EndPoints"];
nw = NestWhile[ImageTake[#, {2, -2}, {2, -2}] & /@ # &, {it1, it, eps}, 
   ComponentMeasurements[#[[3]], "Mask", "BorderComponents" -> False] =!= {} &];

Partition[Framed /@ (ImageMultiply[nw[[1]], #] & /@ (Erosion[#, 1] & /@ Image /@
              (ComponentMeasurements[ColorNegate@nw[[2]], "Mask"][[All, 2]]))), 3] //
              Grid

Mathematica graphics

$\endgroup$
7
  • $\begingroup$ Why does mine return this? postimg.org/image/lain5m35t $\endgroup$
    – Jenny
    Jan 30, 2016 at 20:41
  • $\begingroup$ Additionally, why do the marks change locations from the original board? $\endgroup$
    – Jenny
    Jan 30, 2016 at 20:45
  • $\begingroup$ Probably because you forgot my first line $\endgroup$ Jan 30, 2016 at 20:46
  • $\begingroup$ These are the exact same commands copied and pasted: postimg.org/image/jxj8731p1 $\endgroup$
    – Jenny
    Jan 30, 2016 at 20:48
  • 1
    $\begingroup$ Do you know why the symbols change location in your example? $\endgroup$
    – Jenny
    Jan 30, 2016 at 21:59
4
$\begingroup$

One way to approach this is to use a Watershed algorithm to segment the image. Each of the watersheds contains one of the symbols.

i = Import["https://i.stack.imgur.com/tTeBU.png"];
waterI = WatershedComponents[i];
waterI // Colorize

enter image description here

You can then separate out the three symbols

{m1, m2, m3, m4} = ComponentMeasurements[waterI, "Mask"];
{ImageMultiply[Image[Normal[m1[[2]]]], i], 
 ImageMultiply[Image[Normal[m3[[2]]]], i], 
 ImageMultiply[Image[Normal[m4[[2]]]], i]}

enter image description here

$\endgroup$

Your Answer

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

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