4
$\begingroup$

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.

test image

  • 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.

$\endgroup$
6
$\begingroup$

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

enter image description here

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]
$\endgroup$
  • $\begingroup$ Do you mean from Adobe Illustrator software? $\endgroup$ – Jose E Calderon Nov 1 '18 at 10:02
  • $\begingroup$ @JoseECalderon Yes, edited now in the question. $\endgroup$ – Dunlop Nov 1 '18 at 10:55
  • $\begingroup$ @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! $\endgroup$ – Dunlop Nov 1 '18 at 11:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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