Can Mathematica put these puzzle pieces together?
Assuming polygons follow the same (clockwise or counterclockwise) vertex order, find all good quality two line segment rigid mappings between polygons without overlap with each other (at least much overlap, that is). Construct a graph of these mappings and apply appropriate transforms to polygons by finding transform paths from one polygon to all others. (In this case, these paths are pretty simple).
ReplaceList[MeshPrimitives[#, 2] & /@ meshes, {___,
{a : Polygon[{___, ap : Repeated[_, {3}], ___}]}, ___,
{b : Polygon[{___, bp : Repeated[_, {3}], ___}]}, ___} :>
Module[{err, trans},
{err, trans} =
Chop[FindGeometricTransform[{ap}, Reverse@{bp},
TransformationClass -> "Rigid", Method -> "Linear"], 0.001];
{Property[a \[DirectedEdge] b, "trans" -> trans],
Property[b \[DirectedEdge] a, "trans" -> InverseFunction@trans]} /;
err < 1 &&
Quiet@Area[
RegionIntersection[BoundaryDiscretizeRegion@a,
BoundaryDiscretizeRegion@TransformedRegion[b, trans]]] < 1]] //
With[{g = Graph@Flatten@#},
Graphics[{FaceForm[], EdgeForm@Thick, First@VertexList@g,
GeometricTransformation[#,
Composition @@ (PropertyValue[{g, DirectedEdge @@ #}, "trans"] & /@
Partition[FindShortestPath[g, First@VertexList@g, #], 2, 1])] & /@
Rest@VertexList@g}]] &
For a different approach with image processing, I start with the image of your puzzle.
fig = Import["http://i.stack.imgur.com/xpue6m.jpg"];
comp = MorphologicalComponents[fig // Binarize];
comp = Colorize[comp, ColorFunction -> "Rainbow"];
cols = DominantColors[comp, 20];
blocks = ColorNegate[Binarize@ColorReplace[comp,
Cases[cols, Except[cols[[#]]]], .01]] & /@Range[10];
pieces = blocks[[{1, 6, 7, 8, 9}]]
Now I have my puzzle pieces, I can try to match them. First I fix the centrepiece and try to match other
cen = pieces[[1]];
Now I want to see where does the second piece go. So I use ImageCorrespondingPoints
.
n=2
i1 = cen; i2 = pieces[[2]];
matches = ImageCorrespondingPoints[i1, i2 // ColorNegate];
Show[i1, Graphics[{Red, PointSize[Large], Point[matches[[1]]]}]]
Show[i2, Graphics[{Red, PointSize[Large], Point[matches[[2]]]}]]
The red dots show the corresponding points in each piece. Similarly, you can check the other pieces.
You can crop the images if you want, but make sure the image size remain compatible.