# How to create meshes with multiple regions from 2D images?

I would like to import 2D images to Mathematica and to use them to create meshed regions which can then be used for FEM. I create the images in Adobe illustrator so can either import a vector based image or a bitmap/raster image. Importantly I need to provide different materials properties to the different regions meaning that I need to keep track of which boundary is which.

A typical image would be as follows where we have 3 different regions, and we need to track boundaries between light and medium, light and dark and dark and medium grey. To be consistent of course the boundaries need to match.

• Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.

• Alternatively I can import bitmap images and then use these together with ImageMesh[] however this doesn't seem to work for multiple regions? I only get one region and not multiple ones.

At the moment am stuck and would appreciate any hints about how to solve this.

## 1 Answer

If you don't mind using ImageMesh, you can use simple image processing operations to split the bitmap into separate binary mask images for each shape:

img = Import["https://i.stack.imgur.com/NRoOd.png"];
components =
ComponentMeasurements[ArrayComponents[ImageData@img, 2],
"Mask", #Area > 10000 &];


Explanation: ArrayComponents assigns identical labels to connected components with the same color. ComponentMeasurements can then calculate measurements for each label, and the mask measurement simply returns a binary 1/0 mask. Your image is anti-aliased, so I get a lot of small components at the edges of each shape - that's why I need the #Area>10000 & filter.

This returns a list of labelIndex -> mask elements, which we can then turn into separate meshes:

meshes = components /. (idx_ -> mask_) :> ImageMesh[Image[mask]]


Vector graphics can be imported and visualised, however from illustrator I get spline objects which I cannot seem to mesh correctly.

Can you post a sample spline? This is taken more or less from the documentation and seems to work fine:

g = Graphics[{BSplineCurve[{{0, 0}, {1, -1}, {2, -1}, {3,
0}, {4, -2}, {5, 0}}, SplineClosed -> True], Circle[{2, 1}]}]
BoundaryDiscretizeGraphics[g]

• Do you mean from Adobe Illustrator software? Nov 1, 2018 at 10:02
• @JoseECalderon Yes, edited now in the question. Nov 1, 2018 at 10:55
• @Niki Estner - I understand now Anti-Aliasing I should turn off when exporting a bitmap (forgot about that). In terms of vector images, I see that it works well now, but not when the lines overlap e.g. `g = Graphics[{BSplineCurve[{{0, 0}, {1, -1}, {2, -1}, {3, 0}, {4, -2}, {5, 0}}, SplineClosed -> True], Circle[{2, 0}]}] BoundaryDiscretizeGraphics[g]'. I guess I need to separate the regions. I will have a bit more of a play and see what I can come up with. Thanks for your help! Nov 1, 2018 at 11:01