3

In images like the one shown here, I would like to be able to pick an x,y coordinate and fill the bounded interior with a specified color. That functionality is well known in PhotoShop and GIMP, but I can't seem to find even a simple version of such code for Mathematica. I'm not expecting a great GUI - just some code that I can use and modify.

enter image description here

For[i = 1, i <= 100, i++,
  t[i] = RandomReal[{0, 1}, {20, 2}]];

For[i = 1, i <= 100, i++, g[i] = Graphics[{AbsoluteThickness[1], BezierCurve[t[i], SplineClosed -> True, SplineDegree -> RandomInteger[{4, 16}]]}, PlotRange -> {0, 1}, ImageSize -> {1000, 1000}]; Print[i]; Print[g[i]]]

Youvan
  • 613
  • 3
  • 11

1 Answers1

7

You can remove border components from MorphologicalComponents using DeleteBorderComponents or SelectComponents, colorize and create a mesh object using ImageMesh:

imgMesh = ImageMesh @ Colorize @ DeleteBorderComponents @
 MorphologicalComponents @ Rasterize[#, ImageResolution -> 200] &

Using a random sample of size 9 from OP's g /@ Range[100]:

SeedRandom[1]
Multicolumn[Graphics[{RandomColor[], EdgeForm[Gray], #} & /@ 
     MeshPrimitives[imgMesh @ g @ #, 2], ImageSize -> 250] & /@ 
  RandomSample[Range[100], 9], 3]

enter image description here

We get the same picture using imgMesh2 where

imgMesh2 = ImageMesh @ Colorize @
   SelectComponents[
      MorphologicalComponents @ Rasterize[#, ImageResolution -> 200], 
      #AdjacentBorderCount == 0 &] &;
kglr
  • 394,356
  • 18
  • 477
  • 896