9

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Jenny
  • 559
  • 2
  • 9

2 Answers2

5
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

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
4

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

bill s
  • 68,936
  • 4
  • 101
  • 191