Search code examples
matlabimage-processingsplinenumerical-analysis

Error while generating cubic splines for image processing in MATLAB - "data sites should be distinct"


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?


Solution

  • 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);