Message Boards Message Boards

Create Interactively "VoronoiFying" images?

Posted 8 years ago

There are automatic Voronoi filters or ways to get a Voronoi "effect" for images. See Wolfram Demonstration or another Wolfram Demonstration But I want to do this interactively by determining with the mouse which areas of the image need more or less intense splitting into Voronoi polygons. To get something like this:

example

To achieve this, I first wrote a function to split any Polygon into a number of Voronoi polygons:

voronoiFy[polygon_, n_] := 
 Module[{sites, bounds, vm}, SeedRandom[12345];
  sites = RandomPoint[polygon, n];
  bounds = MinMax /@ Transpose @@ polygon;
  vm = VoronoiMesh[sites, bounds];
  MeshPrimitives[vm, 2]]

and then, I integrated this into a Manipulate that uses a Locator to select a polygon and subsequently split it.

Manipulate[
 Quiet@Module[{polys, activePoly, polys1},(*SeedRandom[1236];*)

   polys = voronoiFy[Polygon[{{0., 0.}, {0, 1}, {1, 1}, {1, 0}}], 
     n1];
   activePoly = SelectFirst[polys, RegionMember[#, pt] &];
   polys1 = 
    Map[RegionIntersection[#, activePoly] &, 
     voronoiFy[activePoly, n2]];
   Graphics[{FaceForm[],
     EdgeForm[{AbsoluteThickness[1], Black}], polys,
     EdgeForm[{AbsoluteThickness[2], Red}], activePoly, polys1}]],
 {{pt, {.55, .5}}, Locator, LocatorRegion -> Full, Appearance -> None},
 {{n1, 16}, Range[6, 24]},
 {{n2, 12}, Range[4, 16]}, Deployed -> True, 
 SynchronousUpdating -> True, TrackedSymbols :> {pt, n1, n2}]

This gives at first this (without any error or pink colouring):

nested Voronoi

But when I select with the Locator another polygon, I get the desired result but only after a flashing pink intermediate image. I tried to catch the error and it says "Voronoi Mesh is not a Graphics Primitive..." I feel it has something to do with the evaluation order but I tried an Evaluate at all possible positions without any result: the pink flash keeps on annoying. Can somebody give a possible solution?

POSTED BY: Erik Mahieu
2 Replies
Posted 8 years ago

I tried to make the function:

result[polygon_, loc_, n1_, n2_] := Module[{polys, activePoly, polys1},
  polys = voronoiFy[Polygon[{{0., 0}, {0, 1}, {1, 1}, {1, 0}}], n1];
  activePoly = SelectFirst[polys, RegionMember[#, loc] &];
  polys1 = 
   RegionIntersection[#, activePoly] & /@ voronoiFy[activePoly, n2];
  {FaceForm[],
   EdgeForm[{AbsoluteThickness[1], Black}], polys,
   EdgeForm[{AbsoluteThickness[2], Red}], activePoly, polys1}]

and put it inside the manipulate but to no avail:

Manipulate[
 Graphics@result[Polygon[{{0., 0}, {0, 1}, {1, 1}, {1, 0}}], pt, n1, 
   n2],
 {{pt, {.55, .5}},(*{0.,0},{1.,1},*)Locator, LocatorRegion -> Full, 
  Appearance -> None},
 {{n1, 16}, Range[6, 24]},
 {{n2, 12}, Range[4, 16]}, Deployed -> True, 
 SynchronousUpdating -> True, TrackedSymbols :> {pt, n1, n2}]

So, maybe it has nothing to do with Evaluation order? Thank for your help...

POSTED BY: Erik Mahieu

Perhaps just save your output in a variable (say called result). Then inside your updating function, just overwrite this variable, rather than saying result = UpdateVoronoi[......]

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

Group Abstract Group Abstract