1

I'm developing an image processing program in MATLAB. One part of it involves sampling some points at fixed intervals from the input image (which is basically a binary line drawing with 1-pixel-wide edges, similar to those made by the "pencil" tool in Paint / GIMP / Photoshop), and then generating a cubic spline with the sampled pixels as its knots.

Now, I know how to generate a cubic spline in MATLAB given a set of (x, y) points, but the problem is this: an image is not a true function of x (say x is the row number of the pixel), because it may have multiple "values" at each x - the values being the numbers of all columns in that row that are black. Because of this MATLAB's spline() function complains that "the data sites should be distinct."

How do I get around this?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
Vicky Chijwani
  • 10,191
  • 6
  • 56
  • 79

1 Answers1

2

I suggest converting your function to parametric form and then doing two splines, on x and y:

   y = f(x) =>   

   y(t),x(t)

In Matlab you can do it in the following way:

  t=1:numel(x);
  xs = interp1(t,x, ti);
  ys = interp1(t,y, ti);

Where xi and yi are the points that you want to interpolate.


Here is an example with data that has multiple y values per x: enter image description here

y = -10:10;
x = y.^2;
figure;plot(x,y,'rv');

t = 1:numel(x);
ti = 1:0.05:numel(x);
xi = interp1(t,x,ti );
yi = interp1(t,y,ti );

hold on ;plot(xi,yi);
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • 2
    Thank you! This seems to suffice for my current needs. As a side-effect I finally understood _why_ the hell parametric forms are important :). We just sort of learnt about them as part of our syllabus a few years ago, but nobody cared to tell us where they'd be useful. – Vicky Chijwani Oct 14 '12 at 17:31