demo10 of Im2mesh package
demo10 - Different polyline smoothing technique
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')
Shape
Let's start demo. Import image Shape.tif.
im = imread('Shape.tif');
if size(im,3) == 3; im = rgb2gray( im ); end
imshow( im,'InitialMagnification','fit' );
Extract boundaries & find control points
We use function getCtrlPnts to find and label control points.
% image to polygon boundary
boundsRaw = im2Bounds( im );
tf_avoid_sharp_corner = false;
boundsCtrlP = getCtrlPnts( boundsRaw, tf_avoid_sharp_corner, size(im) );
plotBounds(boundsCtrlP, true); % show starting and control points
Smooth boundary using Taubin method
threshold_num_turning = 4;
threshold_num_vert_Smo = 4;
boundsTaubin = smoothBounds( boundsCtrlP, lambda, mu, iters, ...
threshold_num_turning, threshold_num_vert_Smo );
plotBounds(boundsTaubin);
Smooth boundary using CCMA method
Let's try another smoothing method - Curvature Corrected Moving Average (CCMA) method (https://github.com/UniBwTAS/ccma). Because Im2mesh package has a good and clear workflow, we can easily implement CCMA method via "smoothBoundsCCMA.m". Function smoothBoundsCCMA is just slightly different from function smoothBounds. Here is some info about the input of function smoothBoundsCCMA.
Let's try it.
threshold_num_turning = 4;
threshold_num_vert_Smo = 4;
boundsCCMA = smoothBoundsCCMA( boundsCtrlP, w_ma, w_cc, ...
threshold_num_turning, threshold_num_vert_Smo );
Plot together
plotBounds2( boundsTaubin, boundsCCMA );
Zoom in
plotBounds2( boundsTaubin, boundsCCMA );
Very good! It seems the result of CCMA method is quite similar to that of Taubin method for our data.
set(groot, 'DefaultFigurePosition', 'factory')