# Color code Voronoi cell areas depending on number of vertices

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:

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"}]}]


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?

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:

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

• Can you please apply it to my data points ... thank you. Is it possible to overplot it with the detected positions? – mrz Mar 15 '16 at 17:31
• 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. – Michael Seifert Mar 15 '16 at 17:38
• Great ... both works – mrz Mar 15 '16 at 17:54
• 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) – mrz Mar 16 '16 at 10:37
• 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.) – Michael Seifert Mar 16 '16 at 14:19

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}]]


• Great ... so I could define my own color function and use it instead of Hue ... – mrz Mar 15 '16 at 18:05
• Exactly! You can define it as you wish. – physicien Mar 15 '16 at 18:16
• 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 – mrz Mar 16 '16 at 10:30