I have an image and use it to generate a Voronoi lattice with following code: SetDirectory[NotebookDirectory[]]; img = Import["1.JPG"]; img = Binarize[ImageResize[img, {1000, 1500}]]; imgt = ImageTrim[img, {{.0, .0}, {1.0, 0.8}}, DataRange -> {{0, 1}, {0, 1}}]; (*distance transform create cell points*) k = DistanceTransform[ColorNegate[imgt]] // ImageAdjust;

ReliefPlot[Reverse@ImageData[k]]; (*To illustrate*)
l = ColorNegate[ Binarize[ColorNegate[LaplacianGaussianFilter[k, 4] //ImageAdjust]]];(*generate voronoi lattice*)
meanValues = ComponentMeasurements[l, {"Centroid"}];
listData = meanValues /. Rule -> List;
listData = Partition[Flatten[listData[[All, 2]]], 2];(*seed center*)
imgBounds = Transpose[{{0, 0}, ImageDimensions[imgt]}];

vm = VoronoiMesh[listData, imgBounds, MeshCellLabel -> {2 -> "Index"}](*mesh region with boundary effects*)

After I got the mesh region and labelled each cell, I want to overlay this mesh lattice to every other images like thisimages.

smallSize = 190;
img1 = Import["2.JPG"];
img1 = Binarize[ImageResize[img1, {1000, 1500}]];
img1 = ImageTrim[img1, {{.0, .0}, {1.0, 0.8}}, DataRange -> {{0, 1}, {0, 1}}];(*imagetrim and import*)
k = DistanceTransform[ColorNegate[img1]] // ImageAdjust;ReliefPlot[Reverse@ImageData[k]]; (*To illustrate*)
l = ColorNegate[Binarize[ColorNegate[LaplacianGaussianFilter[k, 5] // ImageAdjust]]];
img1 = DeleteSmallComponents[Binarize[img1], smallSize];
img1 = DeleteSmallComponents[ColorNegate[img1], smallSize];
img1 = ColorNegate[Binarize[img1]];
img1 = Closing[img1, DiskMatrix[4]];

So img1 is a binary image,which has black and white only. And vm is the mesh region i generate from frist image. I want to overlay the mesh region (vm) to binary image(img1), which can divided img1 into many labelled polygons.The next step I want to count how many black and white pixels in each polygon as a list:

Ploygon ; black pixel number ; white pixel number

  • $\begingroup$ Have you tried anything so far in order to achieve your final goal? $\endgroup$ – MarcoB Jan 23 '17 at 9:13
  • $\begingroup$ yes, i tried overlay two images but since after overlaying images, the voronoi lattice data is missing, I cannot assign pixel positions to voronoi cells $\endgroup$ – Shuoqi Li Jan 23 '17 at 23:07

Here's a starting point that might help you develop your own method. First, I select a sample image and binarize it; then generate some random points to construct a Voronoi mesh:

img = Binarize[ImageResize[ExampleData[{"TestImage", "Apples"}], 600]];

With[{bounds = {1, #} & /@ ImageDimensions[img]},
  pts = Floor@RandomVariate[UniformDistribution[bounds], 10];
  polys = MeshPrimitives[VoronoiMesh[pts, bounds], 2];

   Red, PointSize[0.015], Point@pts,
   EdgeForm[{Thick, Green}], FaceForm[], polys

Mathematica graphics

Above the points are in red, and the resulting mesh is overlaid to the binarized picture.

We then extract the polygons composing the mesh, and generate a series of masks, each one containing a single polygon from the mesh in white. Each mask has the same size as the binarized image.

   {EdgeForm[Thick], polys, White, #},
   ImageSize -> ImageDimensions@img, PlotRangePadding -> None
] & /@ polys

Here is a sample mask:


We now multiply the original image by each one of these masks to isolate the pixels in each polygon; all other pixels are thereby set to black.

     {EdgeForm[Thick], polys, White, #},
     ImageSize -> ImageDimensions@img, PlotRangePadding -> None
] & /@ polys

This would be the result obtained from the sample mask above:

masked image

To count the white pixels, we Binarize the resulting masked pictures, then extract the ImageData array representation, and sum across the array, knowing that white pixels contribute 1; black pixels 0. The total is thus the count of white pixels.

Putting it all together:

        {EdgeForm[Thick], polys, White, #},
        ImageSize -> ImageDimensions@img, PlotRangePadding -> None
    ], 0], 2] & /@ polys

(* Out: {6523, 5791, 4290, 12279, 14486, 3196, 19214, 12937, 19640, 12483} *)
  • $\begingroup$ Thank you very much Marco, it works! yeah~~cheers $\endgroup$ – Shuoqi Li Jan 24 '17 at 23:12
  • $\begingroup$ @ShuoqiLi I'm glad it helped! $\endgroup$ – MarcoB Jan 25 '17 at 0:03

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.