View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Smoothing 2D Contours Using Local Regression Lines

5.0 | 6 ratings Rate this file 31 Downloads (last 30 days) File Size: 3.84 KB File ID: #30793 Version: 1.2
image thumbnail

Smoothing 2D Contours Using Local Regression Lines


Tolga Birdal (view profile)


17 Mar 2011 (Updated )

This submission contains both the implementation and the test function for 2D contour smoothing.

| Watch this File

File Information

A contour of a 2D region is defined by an ordered set of points where the neighboring elements contain the neighboring points. Such representation can be obtained with many techniques such as boundary tracing and chain codes. (In a simple 2D point set or a curve the points do not have to lie in a specific order.)

The contour smoothing is done by projecting all the contour points onto the local regression line. For each point, N neighboring points which lie on the contour are sampled on each side and a local regression line is computed. Then the current point is projected on this line. Applying this algorithm to all the points smooths the contour and in a way brings the points closer. 2N+1 is the number of total points contributing to the computation of the local regression line. The higher the number of points, the smoother the curve.
Because of the linear nature of fitting, when too much smoothing is desired, the algorithm loses important features such as corners, and gets confused in such critical regions. This in a way is a wrong over-smoothing. A way to be less prone to such errors is to use gaussian weighted least squares fit. For this purpose I use Andrey Sokolov's line fitting. Here is the link:

In the test code, a contour is obtained using chain codes (bwtraceboundary) and smoothed. The example is taken from a real application


Weighted Orthogonal Least Squares inspired this file.

Required Products Curve Fitting Toolbox
Image Processing Toolbox
MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
24 Feb 2017 Benjamin Busam

19 May 2016 Lucy Robinson

04 Nov 2013 Tolga Birdal

Tolga Birdal (view profile)

Dear Adil,

Normally, the binary region is assumed to be filled, so that MATLAB's trace methods would work. However, it occurs to me that the problem is not in tracing, but MATLAB cannot locate the initial point to begin trace. This might be due to the mis-selection of initial direction. Can you send me your data?

Comment only
02 Nov 2013 Adil

Adil (view profile)

Does the binary input need to have its objects with boundries only or the object should be completely 1.
In my case it gives me this error:
Attempted to access contour(:,1); index out of bounds because size(contour)=[0,0].

Error in test_smooth_contours (line 39)
[Ys Xs]=smooth_contours(contour(:,1), contour(:,2), 21);

Comment only
19 Apr 2012 Paul Doliotis

20 Jul 2011 Zannatul

20 Jul 2011 Zannatul

20 Mar 2011 Henry Zhu

21 Mar 2011 1.1

Now uses Andrey Sokolov's Weighted Orthogonal Least Squares fit, with gaussian weighting. This is smoother, more robust and less prone to sudden changes such as corners. (If gaussian weighting is not used, it's also faster).

20 Jul 2011 1.2

Description updated

Contact us