# Slice a mathematica image into sub images along black straight lines?

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?

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

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:

• How would I use that to slice directly along the lines thought? Additionally how do I store each image in an array of images? Jan 30, 2016 at 17:42
• Imagine a Tic Tac Toe board. I want to slice exactly along the lines so I can see what is in each position Jan 30, 2016 at 17:45
• You should post sample images Jan 30, 2016 at 18:05
• Probable duplicate: Detecting grid lines in a raster image Jan 30, 2016 at 18:13
• Jan 30, 2016 at 18:14

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


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 /@
Grid


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

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


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]}