demo07 of Im2mesh package
demo07 - Parameter hmax and grad_limit in mesh generation
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.
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.
Set default image size.
x = 250; y = 250; width = 250; height = 250;
set(groot, 'DefaultFigurePosition', [x,y,width,height])
% 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'))
Import image
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' );
hmax = 8
[ 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)|
-------------------------------------------------------
10 361 284
13 373 284
20 381 3867
26 381 4493
Smooth triangulation...
-------------------------------------------------------
|ITER.| |MOVE(X)| |DTRI(X)|
-------------------------------------------------------
10 113 4457
plotMeshes( vert, tria, tnum )
We can see that the max mesh size is limited by the parameter hmax.
hmax = 500
Let's use a much larger hmax to see how things are changing.
[ 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)|
-------------------------------------------------------
10 224 4067
20 12 4067
plotMeshes( vert, tria, tnum )
Now, the max mesh size is much larger. Note that hmax is a upper bound for the max mesh size. There exists boundary edge contraints and gradient-limt, so the max mesh size in the generated mesh can not reach hmax.
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)|
-------------------------------------------------------
10 32 2500
20 1 2500
30 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 )
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 = 0.25
Let's use a smaller grad_limit to see how things are changing.
[ 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)|
-------------------------------------------------------
10 224 4067
20 12 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!
set(groot, 'DefaultFigurePosition', 'factory')