3
$\begingroup$

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]]] 
$\endgroup$
4
  • 3
    $\begingroup$ Is the input a Graphics or an Image object? $\endgroup$ Commented Mar 6, 2021 at 17:13
  • $\begingroup$ @kglr Graphics. I added some code to the figure. Watch out for the 100 iterations. $\endgroup$ Commented Mar 6, 2021 at 18:55
  • 1
    $\begingroup$ After getting the jaggies with Rasterize, I tried this and it works nicely: labels = MorphologicalComponents[g[1]] Colorize[labels] $\endgroup$ Commented Mar 6, 2021 at 20:55
  • $\begingroup$ A related question: (41118) $\endgroup$ Commented Apr 7, 2021 at 17:15

1 Answer 1

7
$\begingroup$

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 &] &; 
$\endgroup$
1
  • $\begingroup$ Those are beautiful! Thank you. $\endgroup$ Commented Mar 9, 2021 at 0:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.