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
  • $\begingroup$ How would I use that to slice directly along the lines thought? Additionally how do I store each image in an array of images? $\endgroup$
    – Jenny
    Commented Jan 30, 2016 at 17:42
  • $\begingroup$ Imagine a Tic Tac Toe board. I want to slice exactly along the lines so I can see what is in each position $\endgroup$
    – Jenny
    Commented Jan 30, 2016 at 17:45
  • 1
    $\begingroup$ You should post sample images $\endgroup$ Commented Jan 30, 2016 at 18:05
  • 1
    $\begingroup$ Probable duplicate: Detecting grid lines in a raster image $\endgroup$ Commented Jan 30, 2016 at 18:13
  • 1
    $\begingroup$ Another: How to detect crosses and circles in 60x60 raster images? $\endgroup$ Commented Jan 30, 2016 at 18:14

2 Answers 2

5
$\begingroup$
Framed[i = Import["https://i.sstatic.net/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
    Commented Jan 30, 2016 at 20:41
  • $\begingroup$ Additionally, why do the marks change locations from the original board? $\endgroup$
    – Jenny
    Commented Jan 30, 2016 at 20:45
  • $\begingroup$ Probably because you forgot my first line $\endgroup$ Commented Jan 30, 2016 at 20:46
  • $\begingroup$ These are the exact same commands copied and pasted: postimg.org/image/jxj8731p1 $\endgroup$
    – Jenny
    Commented Jan 30, 2016 at 20:48
  • 1
    $\begingroup$ Do you know why the symbols change location in your example? $\endgroup$
    – Jenny
    Commented 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.sstatic.net/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 you have read our privacy policy.

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