B-spline fitting to 2D discrete data points (pixels of contour image)

27 views (last 30 days)
I am trying to fit a B-spline to a set of ordered discrete data points which represent pixels of a contour extracted from a image.
The below code works fine for some simple shapes, but not for others (please see image here http://imgur.com/5IOVmEt
for examples). Why does this happen, and what would be a better way to approach this problem?
I am quite new to differential geometry and MATLAB in general, appreciate any insights or inputs. Thanks.
x = data(:, 1); y = data(:, 2); % data contains two columns representing x,y coordinates
plot(x, y, 'bo');
fittedmodel = fit(x, y, 'cubicinterp');
plot(fittedmodel, 'r-');

Accepted Answer

Image Analyst
Image Analyst on 6 May 2013
Looks to me like the problem is you sorted your x coordinates and didn't keep them in the order that they were are you traverse around the contour.
Why are you using splines? Do you want to interpolate at a higher resolution and keep the curve smooth and make sure it still goes through each original point? If so, use splines. If you want to smooth the boundary so there are less sharp jaggy edges and spikes and don't care if it goes exactly through each original point, then you can do other things, like perhaps a Savitzky-Golay filter (the sgolay function in the Signal Processing Toolbox).
  6 Comments
Image Analyst
Image Analyst on 3 Sep 2013
I don't have a B spline demo. Never needed one before since the regular cubic spline always worked good enough.
Evripidis
Evripidis on 3 Sep 2013
Thanks for your quick reply! I suppose I will use cubic one because de Boor algorithm for b splines is too complex for me right now

Sign in to comment.

More Answers (1)

korakuti hanumanth
korakuti hanumanth on 2 Jan 2017
Hi community,
I have two vectors and I would like to fit a B spline curve to:
y=[18.93000031 19.42000008 19.51000023 19.67000008 19.68000031 19.71999931];
x=[58.61111111 67.32055556 70.56194444 74.22694444 78.39388889 85.11555556];
I would also like to save the parameters of the fit at every point to use in regression analysis and also explain how to find the coefficients of b spline curve and some error techniques to satisfie the fitting.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!