demo07 of Im2mesh package

demo07 - Parameter grad_limit for mesh generation
Cite as: Ma, J., & Li, Y. (2025). Im2mesh: A MATLAB/Octave package for generating finite element mesh based on 2D multi-phase image (2.1.5). Zenodo. https://doi.org/10.5281/zenodo.14847059
Jiexian Ma, mjx0799@gmail.com. Project website.
Table of Contents

Note

I suggest familiarizing yourself with Im2mesh_GUI before learning Im2mesh package. With graphical user interface, Im2mesh_GUI will help you better understand the workflow and parameters of Im2mesh package.
Im2mesh_GUI: https://www.mathworks.com/matlabcentral/fileexchange/179684-im2mesh_gui-2d-image-to-finite-element-mesh
If you are using Im2mesh package in MATLAB, you need to install MATLAB Image Processing Toolbox and Mapping Toolbox because Im2mesh package use a few functions in these toolboxes.

Setup

Before we start, please set folder "Im2mesh_Matlab" as your current folder of MATLAB.
clearvars
Set default image size.
x = 250; y = 250; width = 250; height = 250;
set(groot, 'DefaultFigurePosition', [x,y,width,height])
% To reset:
% set(groot, 'DefaultFigurePosition', 'factory')
Function im2mesh use a mesh generator called MESH2D (developed by Darren Engwirda). We can use the following command to add the folder 'mesh2d-master' to the path of MATLAB.
addpath(genpath('mesh2d-master'))

Transition

Let's start demo. Import image Transition.tif.
im = imread('Transition.tif');
if size(im,3) == 3; im = rgb2gray( im ); end
imshow( im,'InitialMagnification','fit' );
Generate mesh.
opt = []; % reset opt
opt.threshold_num_turning = 0;
opt.threshold_num_vert_Smo = 0;
opt.threshold_num_vert_Sim = 0;
opt.grad_limit = 0.5;
[ vert, tria, tnum ] = im2mesh( im, opt );
Refine triangulation... ------------------------------------------------------- |ITER.| |CDT1(X)| |CDT2(X)| ------------------------------------------------------- 4 160 284 10 202 882 16 203 989 Refine triangulation... ------------------------------------------------------- |ITER.| |CDT1(X)| |CDT2(X)| ------------------------------------------------------- 5 276 284 10 288 1967 20 294 2549 22 295 2556 Smooth triangulation... ------------------------------------------------------- |ITER.| |MOVE(X)| |DTRI(X)| ------------------------------------------------------- 4 804 2500 8 133 2500 12 11 2500 16 1 2500 20 1 2500 24 1 2500 28 1 2500 32 1 2500
plotMeshes( vert, tria, tnum )
You can see the mesh is very dense in some region. Let's zoom in to take a look.
plotMeshes( vert, tria, tnum )
xlim([79 184])
ylim([34 134])
We can evaluate the mesh quality.
tricost( vert, tria, tnum );
The mean value of Q is 0.94. The minimum Q is 0.537.

grad_limit

Let's use a smaller grad_limit to see how things are changing.
opt = []; % reset opt
opt.threshold_num_turning = 0;
opt.threshold_num_vert_Smo = 0;
opt.threshold_num_vert_Sim = 0;
opt.grad_limit = 0.25;
[ vert, tria, tnum ] = im2mesh( im, opt );
Refine triangulation... ------------------------------------------------------- |ITER.| |CDT1(X)| |CDT2(X)| ------------------------------------------------------- 4 160 284 10 202 882 16 203 989 Refine triangulation... ------------------------------------------------------- |ITER.| |CDT1(X)| |CDT2(X)| ------------------------------------------------------- 7 330 284 10 333 1832 20 338 4107 20 338 4107 Smooth triangulation... ------------------------------------------------------- |ITER.| |MOVE(X)| |DTRI(X)| ------------------------------------------------------- 4 1225 4067 8 503 4067 12 93 4067 16 24 4067 20 12 4067 24 1 4067
plotMeshes( vert, tria, tnum )
We can evaluate the mesh quality again.
tricost( vert, tria, tnum );
Now, the new Q mean is 0.964. The minimum Q is 0.7. Very good!
% reset image size
set(groot, 'DefaultFigurePosition', 'factory')
% end of demo