# Recognize simple shapes in an image and color them randomly?

I have an image of polygons built from a polar function (see below). I'd like to color each triangle or quadrilateral a random color.

Firstly, I'm not sure how to separate the simple shapes.

Secondly, I can't think of a simple way to color each a random color (only 3 different colors result with Colorize).

sunflower = 2 Pi (1 - 1/GoldenRatio);
PolarCoordinate[r_, theta_] := r {Cos[theta], Sin[theta]}
Graphics[Polygon[
Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 1000}]],
AspectRatio -> 1] // Colorize • A simple first approach could be p = Graphics[ Polygon[Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 500}]], ImageSize -> 1000]; MorphologicalComponents[ColorNegate@Dilation[p, 2], CornerNeighbors -> False] // Colorize – Dr. belisarius Jan 26 '14 at 4:24

## 2 Answers

Something like this (there's many ways to skin this cat) will do it. Play with parameters to your liking:

SelectComponents[MorphologicalComponents[yourImageHere, .8], "Area",
10^9] // Colorize


Putting this with the excellent linearization idea of Pickett in the comments, we can get this pleasing result:

sunflower = 2 Pi (1 - 1/GoldenRatio);
PolarCoordinate[r_, theta_] := r {Cos[theta], Sin[theta]}
p = Graphics[
Line[Table[PolarCoordinate[i^10, i*sunflower], {i, 1, 1000}]],
AspectRatio -> 1];

SelectComponents[MorphologicalComponents[p, .89], "Area",
10^3] // Colorize • ...where yourImageHere is the OP's plot but where Polygon has been replaced by Line. – C. E. Jan 26 '14 at 4:40
• @Pickett: Good idea, cleans up result nicely! – ciao Jan 26 '14 at 4:44
• Brilliant! Is it possible to change the color palette (ColorFunction(?))? Note that Colorize[yourImageHere,ColorFunction->"Rainbow"] ruins the stained glass randomness. – user8454 Jan 26 '14 at 4:50
• @user8454: Sure, you can just replace the Colorize above with something like Colorize[#, ColorFunction -> "Rainbow"] &. You'll probably want to play around with parameters, and perhaps implement your own ColorFunction to get the results you want. – ciao Jan 26 '14 at 4:53
• @rasher, it's probably easier to modify the component matrix than the color function. e.g. Colorize[Mod[863 #, 231], ColorFunction -> "Rainbow"] & – Simon Woods Jan 26 '14 at 11:52

This method can be very time-consuming, and the scale of the original graphics seems need be small (thus i^10/10^30), but yes you can do it in vectorgraph way, with the help of Region functions described here.

sunflower = 2 Pi (1 - 1/GoldenRatio);
PolarCoordinate[r_, theta_] := r {Cos[theta], Sin[theta]}
poly = Polygon[Table[PolarCoordinate[i^10/10^30, i*sunflower], {i, 900, 1000}]] // N;

GraphicsRegionRegionInit[];

simplePolySet = SimplePolygonPartition[poly];

Graphics[
{EdgeForm[White], ColorData["DarkRainbow"][RandomReal[]], #} & /@
simplePolySet (*uncomment to manipulate them:*)(* /.
Polygon[pts__] :>
GeometricTransformation[
Polygon[pts],
TranslationTransform[Norm[Mean[pts]]^5 Normalize[Mean[pts]]]
]*)
] • Nicely done - I tried SimplePolygonPartition but gave up after it just sat there thinking for ten minutes. – Simon Woods Jan 28 '14 at 9:27
• @SimonWoods Thanks :) I guess when the scale is too large (here $10^{30}$), this function will stuck because of some inner parts (in fact, IntersectQ, who used something like GraphicsMeshDeveloperCreateMesh ) using machine number. – Silvia Jan 28 '14 at 9:42