Browsing though the documentation of ImageMesh I found it lacking with regard to its methods and options concerning mesh generation. In my application I have an image with very fine details (compared to the size of the image as a whole) as a Graphics object. How does one increase the precision used to generate the mesh?


ImageMesh is designed to work with binary Images only, hence it automatically rasterizes Graphics objects by applying Image and Binarize to them:

Trace[ImageMesh@Graphics[{Black, Disk[], White, RegularPolygon[1, 5]}],
 Image[_Graphics] | _Binarize]


Due to the above reason, you should compare the obtained mesh not with Graphics, but with the result of rasterization:

Grid[Partition[Table[Framed@Grid[{{s, SpanFromLeft},
        {Image[Binarize@#, ImageSize -> 100],
         ImageMesh[#, Method -> "MarchingSquares", ImageSize -> 100]}}] &@
    Graphics[{Black, Disk[], White, RegularPolygon[1, 5]}, 
     ImageSize -> s], {s, 20, 80, 5}], 3]]


By the way, the Documentation statement

ImageMesh works with arbitrary 2D and 3D images, taking the nonzero pixels of the image as foreground.

isn't quite correct as you can see from the above Trace output: actually for 2D images the binarization is performed using Otsu's algorithm.


Even though Graphics are not exactly pixel images but rather more like vector images, setting ImageSize does have an effect on the mesh quality.

Take for instance

Region[#, PlotLabel -> s] &@
ImageMesh[#, Method -> "MarchingSquares"] &@
Graphics[{Black, Disk[], White, RegularPolygon[1, 5]}, 
ImageSize -> s], {s, 20, 300, 1}]


I'd personally prefer there to be an explicit method option for ImageMesh, e.g. "MeshPrecision" for use with Graphics; this does work for my application just fine though.

  • $\begingroup$ See also this Q&A $\endgroup$
    – Sascha
    Nov 1 '17 at 10:01

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.