6
$\begingroup$

The following question is similar to Colouring points in a Delaunay Mesh by the number of nearest neighbours ... but I do not know how adapt this answer to my problem.

I have the following image:

enter image description here

For detecting the white dots and displaying the Voronoi mesh without edges I use:

meanValues = 
  ComponentMeasurements[image,{"Centroid"}];
listData = meanValues /. Rule -> List;
listData = Partition[Flatten[listData[[All, 2]]], 2];
vm = VoronoiMesh[listData];
Graphics[{LightBlue, EdgeForm[Black], 
  MeshPrimitives[vm, {2, "Interior"}]}]

enter image description here

Now I would like to display the Voronoi cell areas (overlayed with the image or/and together with ListPlot of the points listData) and colorize them in such a way that different colors are used for different number of corners corresponding to a Voronoi cell (e.g. 4 corners: green, 5 corners: blue, etc. ).

How can I do that?

$\endgroup$
14
$\begingroup$

MeshPrimitives returns a list of Polygon objects; and it's not too tricky to just to count the number of points in each Polygon:

colorvm = Map[{ColorData[97, First[Dimensions[First[#]]] - 2], #} &,  MeshPrimitives[vm, {2, "Interior"}]]]
Graphics[{EdgeForm[Black], colorvm}]

Here's what it returns on the Voronoi diagram for a bunch of random points:

enter image description here

The number 97 is used to access the default plot colors for MM 10, but other color schemes are available as well.

$\endgroup$
  • $\begingroup$ Can you please apply it to my data points ... thank you. Is it possible to overplot it with the detected positions? $\endgroup$ – mrz Mar 15 '16 at 17:31
  • 1
    $\begingroup$ I don't have time to download & import your image just now; but if you add those two lines of code to the code you provided, it should work just fine. If you want to add the points to the graphic, use Graphics[{EdgeForm[Black], colorvm, Point[listData]}] as the last line instead of the one I used above. $\endgroup$ – Michael Seifert Mar 15 '16 at 17:38
  • $\begingroup$ Great ... both works $\endgroup$ – mrz Mar 15 '16 at 17:54
  • $\begingroup$ How would I use here my own color table colTable = {Yellow, Brown, Green, LightGray, Red, Blue}; instead of 97?. Yellow: 3 vortices, Brown: 5 vortices, etc. (see below the comment to answer from physicien) $\endgroup$ – mrz Mar 16 '16 at 10:37
  • 1
    $\begingroup$ If you want to use your own color table, replace the first line of my code with colorvm = Map[{colTable[[First[Dimensions[First[#]]] - 2]], #} &, MeshPrimitives[vm, {2, "Interior"}]]. Note, though, that this will issue an error if your color table doesn't have enough entries (in your case, if any cell has more than 8 neighbors.) $\endgroup$ – Michael Seifert Mar 16 '16 at 14:19
4
$\begingroup$

You can change the color function, but the idea is there.

meanValues = ComponentMeasurements[image, {"Centroid"}];
listData = meanValues /. Rule -> List;
listData = Partition[Flatten[listData[[All, 2]]], 2];
vm = VoronoiMesh[listData];

meshData = MeshPrimitives[vm, {2, "Interior"}];
iterMax = Length@meshData;
nbSideCell = Length @@@ meshData;
colorVect = Red;

Graphics[Table[{Hue[1/nbSideCell[[i]]], EdgeForm[Black], 
meshData[[i]]}, {i, iterMax}]]

a busy cat

$\endgroup$
  • 1
    $\begingroup$ Great ... so I could define my own color function and use it instead of Hue ... $\endgroup$ – mrz Mar 15 '16 at 18:05
  • $\begingroup$ Exactly! You can define it as you wish. $\endgroup$ – physicien Mar 15 '16 at 18:16
  • $\begingroup$ I used for my own colTable = {Yellow, Brown, Green, LightGray, Red, Blue}; and then Graphics[Table[{colTable[[nbSideCell[[i]]-2]], EdgeForm[Black], meshData[[i]]}, {i, iterMax}]], which works fine $\endgroup$ – mrz Mar 16 '16 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.