File Exchange

image thumbnail

Im2mesh (2D image to triangular meshes)

version 0.8.2 (120 KB) by Jiexian Ma
Transfer 2d multi-phase image into FEM meshes, with poly-line simplification feature.


Updated 21 Jul 2019

View License

This tool consists of a few functions, like, im2Bounds (image to polygonal boundaries), getCtrlPnts (mark intersecting vertex between polygons, serving as fixed point for polygon simplification and meshing), simplifyBounds (simplify polygon), poly2mesh (polygon to triangular meshes), getInterf (search nodes at interface), printInp (export as Inp file). The Inp file can be imported into software Abaqus.

This tool was originally written in March 2018 for particle material with air void. The code was rewritten in 2019 so it can work for multi-phase materials. Exactly reserve the contact detail between different phases.

Note: To make this code run (especially for poly2mesh), you need to download MESH2D from, and add the folder (mesh2d-master) to your path. After that, you can start with demo() in im2mesh, and try to vary the parameter - tolerance to see its effects on the result. Check the images in the folder (im2mesh\examples\...).

If you have any questions or suggestions, feel free to send me an email., July 2019.

Cite As

Jiexian Ma (2019). Im2mesh (2D image to triangular meshes) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)


@Jiexian: Got it! This is helpful - thanks. FYI: I have tweaked it to just select which specific gray levels we want to mesh (and which to ignore).

Nice work and helpful code. Thanks!

Jiexian Ma

@Seth, yep, I know your meaning.
You can replace line 19 to 21 in im2Bounds.m with following code. It should work.
nlevels = 1;
bounds = cell( 1, nlevels );
intensity = max(max(im));


Is there a simple way to provide the mesh of just a single object in the image instead of meshing the full image space? For example, you provide an example image titled "a1.tif". Is it easy to just mesh the white object instead of both the white and black space? (Say I wanted to build a solid model in Abaqus of this white object, but the black color is just air and is not usually modeled).

This may also be helpful in the cases of very large images that require a long time to mesh. In that case, I don't want to waste resources on meshing the background of the image.

Many thanks!

I tried the code by running the "demo.m" file. There is an Abaqus input file, "test.inp", generated. However, when I import the inp file to ABAQUS CAE or Hypermesh, both of them complain about errors. Would you please double check the formatting of the inp file?



new image for cover


revise getCtrlPnts() for time efficiency


revise printInp(), support linear element and quadratic element
revise im2Bounds() and bwperimtrace(), for 4-connected neighbourhood
able to clear up redundant vertices that are brought about by delZeroAreaPoly


reorganize part of the code, increase readability
revise some comments
add calculation of polygon's area, polygon with zero area will be deleted automatically
update examples


update description


update description

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux