Group Abstract Group Abstract

Message Boards Message Boards

[Hackathon] Tic-Tac-Toe with autonomous Delta Robot

POSTED BY: Jenny Mandl
2 Replies

Hello! It sounds like you are trying to identify and find the location of the objects on a tic tac toe board. Here is a notebook which does that. I do not have your exact lighting conditions, so I used an intentionally bad tic tac toe board made in paint. It definitely is not perfect, but maybe it will give you some ideas.

The image I used to test (high-res in the notebook), intentionally bad paint image to try and replicate less than ideal conditions

enter image description here

(*Smoothing out the image, and getting components. This is similar to the original post. Ideally the x's, o's, and grid are separate.*)

srcAdjusted = Erosion[img, DiskMatrix[5]];
components = ComponentMeasurements[ColorNegate@Binarize[srcAdjusted], {"ConvexArea", "Mask"}][[All, 
   2]];

(*Getting the grid component, filling in the center*)

largestComponent = Image[SortBy[components, "First"][[-1, 2]]];
mask = FillingTransform[Closing[largestComponent, 3]]

(*subtracting the component from the filled in center to get just the box, then finding the corners of it*)

box = Opening[ImageSubtract[mask, largestComponent], DiskMatrix[3]];
corners = ImageCorners[box, 20, .01, 25, MaxFeatures -> 4];
Show[box, ListPlot[corners]]

(*remove the grid from components, and obtain the image equivalent of each sparse array*)

nogrid = Delete[SortBy[components, First], -1];
imgcomponents = Map[Image[#[[2]] // Normal] &, nogrid]

(*Sort the corners, get the furthest left/right/top/bottom*)

sortx = SortBy[corners, #[[1]] &];
sorty = SortBy[corners, #[[2]] &];
{left, top, right, bottom} = {Mean[{sortx[[1, 1]], sortx[[2, 1]]}], 
  Mean[{sorty[[3, 2]], sorty[[4, 2]]}], Mean[{sortx[[3, 1]], sortx[[4, 1]]}], 
  Mean[{sorty[[1, 2]], sorty[[2, 2]]}]}

(*Obtain the center of each, figure out what space on the tictactoe board it should be*)

centerOfGravity[l_] := ComponentMeasurements[Image[l], "Centroid"][[1, 2]]
getrowcol[{x_, y_}]  := {If[y >= top, 1, If[y >= bottom, 2, 3]], 
  If[x <= left, 1, If[x <=  right, 2, 3]]  }
locations = Map[{getrowcol[centerOfGravity[#]], 
    If[Length[ImageLines[#, .18, .15]] > 1, "x", "o"]} &, imgcomponents ]

(*Make an empty array, map in the above data, display it in matrix form*)

tictactoe = {{"", "", ""}, {"", "", ""}, {"", "", ""}};
Map[(tictactoe[[#[[1, 1]], #[[1, 2]]]] = #[[2]]) &, locations];
tictactoe // MatrixForm

enter image description here

Attachments:
POSTED BY: Garrett Ducharme

Hello, I edited the post. The python was the tic tac toe algorithm and the goal was to have mathematica interface between the python and the arduino controlling the delta robot which is a 3 axis arm typically used for pick and place operations in manufacturing (we made a fully functioning one with 3D printed parts and stepper motors)

POSTED BY: Jenny Mandl
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard