demo06 of Im2mesh package
demo06 - Thresholds in polyline smoothing
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')
Circle
Let's start demo. Import image Circle.tif.
im = imread('Circle.tif');
if size(im,3) == 3; im = rgb2gray( im ); end
imshow( im,'InitialMagnification','fit' );
Extract boundaries and find control points
% image to polygon boundary
boundsRaw = im2Bounds( im );
tf_avoid_sharp_corner = false;
boundsCtrlP = getCtrlPnts( boundsRaw, tf_avoid_sharp_corner, size(im) );
Smooth boundary
Set thresholds to zero.
threshold_num_turning = 0;
threshold_num_vert_Smo = 0;
boundsSmooth = smoothBounds( boundsCtrlP, lambda, mu, iters, ...
threshold_num_turning, threshold_num_vert_Smo );
plotBounds(boundsSmooth);
It seems that some boundaries are over-smoothed. Let's zoom in to check that.
plotBounds(boundsSmooth);
Compare
We can compare boundsSmooth with boundsRaw. Here, we use function plotBounds2 to plot these two boundaries in the same figure.
plotBounds2( boundsRaw, boundsSmooth );
Let's zoom in.
plotBounds2( boundsRaw, boundsSmooth );
We can see that boundsSmooth lost the details of the pristine boundaries. Some of the smoothed polygons have zero area. This is not what we want.
Set threshold
Now, we set thresholds.
threshold_num_turning = 10;
threshold_num_vert_Smo = 20;
boundsSmooth = smoothBounds( boundsCtrlP, lambda, mu, iters, ...
threshold_num_turning, threshold_num_vert_Smo );
plotBounds(boundsSmooth);
Zoom in.
plotBounds(boundsSmooth);
It seems new boundaries are better. We can compare with the pristine boundaries.
plotBounds2( boundsRaw, boundsSmooth );
Very good.
set(groot, 'DefaultFigurePosition', 'factory')