0
votes

Essentially, I am trying to smooth a contour line from an image segmentation using Matlab's interp1 method. Unfortunately, interp1 is not behaving as I expect, perhaps because I am using it incorrectly. My code for the interpolation follows:

y2 = interp1(x, y, 'nearest');

Then, I attempted to plot the original x value against y2 along with the original function (see attached plot).

plot(x, y2, 'x');

I thought that the interpolation would smooth the original function - which, to some extent, it does - yet, the interp1 method appears to be offset from the origin and rotated. I would like this result more aligned with the original data. Any guidance would be appreciated.

figure

+------------------+------------------+
|       y          |        x         |
+------------------+------------------+
| 37.9838709677419 | 1                |
| 38               | 1.02500000000000 |
| 38.9750000000000 | 2                |
| 38.9935064935065 | 3.00000000000000 |
| 39               | 3.08333333333333 |
| 39.9166666666667 | 4                |
| 39.9935064935065 | 5.00000000000000 |
| 40               | 5.01562500000000 |
| 40.9843750000000 | 6                |
| 40.9928571428571 | 7                |
| 41               | 7.01388888888889 |
| 41.9861111111111 | 8                |
| 42               | 8.01388888888889 |
| 42.9861111111111 | 9                |
| 42.9966216216216 | 10               |
| 43               | 10.0055555555556 |
| 43.9944444444445 | 11.0000000000000 |
| 44               | 11.0384615384615 |
| 44.9615384615385 | 12               |
| 44.9948979591837 | 13               |
| 45               | 13.0555555555556 |
| 45.9444444444444 | 14               |
| 45.9848484848485 | 15               |
| 46               | 15.0333333333333 |
| 46.9666666666667 | 16               |
| 46.9916666666667 | 17               |
| 46.9939759036145 | 18               |
| 47               | 18.0357142857143 |
| 47.9642857142857 | 19               |
| 47.9861111111111 | 20               |
| 48               | 20.0098039215686 |
| 48.9901960784314 | 21.0000000000000 |
| 49               | 21.0074626865672 |
| 49.9925373134328 | 22               |
| 50               | 22.0076923076923 |
| 51               | 22.1000000000000 |
| 51.9000000000000 | 23               |
| 52               | 23.0500000000000 |
| 52.9500000000000 | 24.0000000000000 |
| 53               | 24.0161290322581 |
| 53.9838709677419 | 25               |
| 54               | 25.0217391304348 |
| 54.9782608695652 | 26               |
| 55               | 26.1666666666667 |
| 55.8333333333333 | 27               |
| 55.9886363636364 | 28               |
| 56               | 28.0087719298246 |
| 56.9912280701754 | 29               |
| 56.9935897435897 | 30               |
| 57               | 30.1000000000000 |
| 57.9000000000000 | 31.0000000000000 |
| 57.9722222222222 | 32               |
| 58               | 32.1250000000000 |
| 58.8750000000000 | 33               |
| 58.9782608695652 | 34               |
| 58.9687500000000 | 35               |
| 58.9883720930233 | 36.0000000000000 |
| 59               | 36.0833333333333 |
| 59.9166666666667 | 37               |
| 59.9736842105263 | 38               |
| 60               | 38.1000000000000 |
| 60.9000000000000 | 39               |
| 60.9750000000000 | 40               |
| 61               | 40.0113636363636 |
| 61.9886363636364 | 41               |
| 61.9883720930233 | 42               |
| 62               | 42.0714285714286 |
| 62.9285714285714 | 43               |
| 63               | 43.0357142857143 |
| 64               | 43.0714285714286 |
| 65               | 43.1000000000000 |
| 66               | 43.0833333333333 |
| 67               | 43.0833333333333 |
| 68               | 43.0625000000000 |
| 69               | 43.0384615384615 |
| 70               | 43.0166666666667 |
| 71               | 43.0106382978723 |
| 71.1666666666667 | 43               |
| 72               | 42.1666666666667 |
| 73               | 42.0833333333333 |
| 74               | 42.0625000000000 |
| 75               | 42.0625000000000 |
| 76               | 42.1666666666667 |
| 76.8333333333333 | 43               |
| 77               | 43.0142857142857 |
| 78               | 43.0277777777778 |
| 78.9722222222222 | 44               |
| 79               | 44.0064102564103 |
| 79.9935897435898 | 45               |
| 80               | 45.0277777777778 |
| 81               | 45.2500000000000 |
| 81.7500000000000 | 46               |
| 82               | 46.0833333333333 |
| 82.9166666666667 | 47.0000000000000 |
| 83               | 47.0166666666667 |
| 84               | 47.1250000000000 |
| 84.8750000000000 | 48               |
| 85               | 48.0131578947368 |
| 86               | 48.0714285714286 |
| 86.9285714285714 | 49               |
| 87               | 49.5000000000000 |
| 87.5000000000000 | 50               |
| 88               | 50.0714285714286 |
| 88.9285714285714 | 51.0000000000000 |
| 89               | 51.0178571428571 |
| 89.9821428571429 | 52               |
| 89.9583333333333 | 99               |
| 89.9923076923077 | 100              |
| 89.9938271604938 | 101              |
| 89.9666666666667 | 102              |
| 89.9375000000000 | 103              |
| 90               | 52.0106382978723 |
| 90.9893617021277 | 53               |
| 90.9750000000000 | 85               |
| 90.9915254237288 | 86               |
| 90.9871794871795 | 87               |
| 90.9687500000000 | 88               |
| 90.9583333333333 | 89               |
| 90.9935064935065 | 90               |
| 90.9937500000000 | 91               |
| 90.9807692307692 | 92               |
| 90.9687500000000 | 93               |
| 90.9827586206897 | 94               |
| 90.9814814814815 | 95               |
| 90.9500000000000 | 96.0000000000000 |
| 90.9687500000000 | 97               |
| 90               | 98.9444444444444 |
| 90.9444444444444 | 98               |
| 90               | 103.062500000000 |
| 90.9375000000000 | 104              |
| 90.8333333333333 | 105              |
| 91               | 53.0166666666667 |
| 91.9833333333333 | 54.0000000000000 |
| 91.9687500000000 | 75               |
| 91.9545454545455 | 77               |
| 91.9642857142857 | 78.0000000000000 |
| 91.9444444444444 | 79               |
| 91.8333333333333 | 80               |
| 91.9166666666667 | 81               |
| 91.9000000000000 | 82               |
| 91.5000000000000 | 83               |
| 91               | 84.7500000000000 |
| 91.7500000000000 | 84               |
| 91               | 105.100000000000 |
| 91.9000000000000 | 106              |
| 91.9615384615385 | 107              |
| 91.9500000000000 | 124              |
| 91.9000000000000 | 125              |
| 91.7500000000000 | 126              |
| 91.8750000000000 | 127              |
| 91.9000000000000 | 128              |
| 91.8750000000000 | 129              |
| 92               | 54.0294117647059 |
| 92.9705882352941 | 55               |
| 92.9910714285714 | 56               |
| 92               | 74.9687500000000 |
| 92.9687500000000 | 74               |
| 92               | 75.0416666666667 |
| 92               | 76.9583333333333 |
| 92.9583333333333 | 76.0000000000000 |
| 92               | 107.125000000000 |
| 92.8750000000000 | 108              |
| 92.9687500000000 | 109              |
| 92.9500000000000 | 122.000000000000 |
| 92               | 123.875000000000 |
| 92.8750000000000 | 123              |
| 92               | 129.125000000000 |
| 92.8750000000000 | 130              |
| 93               | 56.0125000000000 |
| 93.9750000000000 | 69.0000000000000 |
| 93.9838709677419 | 70               |
| 93.9761904761905 | 71               |
| 93.9761904761905 | 72.0000000000000 |
| 93               | 73.9500000000000 |
| 93.9500000000000 | 73               |
| 93               | 109.071428571429 |
| 93.9285714285714 | 110.000000000000 |
| 93               | 121.968750000000 |
| 93.9687500000000 | 121              |
| 93               | 130.083333333333 |
| 93.9166666666667 | 131              |
| 94               | 56.0625000000000 |
| 94.9375000000000 | 57               |
| 94.9838709677419 | 58               |
| 94.9864864864865 | 59               |
| 94.9893617021277 | 66               |
| 94.9722222222222 | 67               |
| 94.0000000000000 | 68.9833333333333 |
| 94.9833333333333 | 68               |
| 94               | 110.062500000000 |
| 94.9375000000000 | 111              |
| 94.9861111111111 | 112.000000000000 |
| 94.9861111111111 | 115.000000000000 |
| 94.9933333333333 | 119              |
| 94               | 120.980769230769 |
| 94.9807692307692 | 120              |
| 94               | 131.012820512821 |
| 94.9871794871795 | 132              |
| 95               | 59.0384615384615 |
| 95.9615384615385 | 60               |
| 95.9807692307692 | 61.0000000000000 |
| 95.9722222222222 | 62               |
| 95.9838709677419 | 63               |
| 95.9852941176471 | 64               |
| 95               | 65.9583333333333 |
| 95.9583333333333 | 65               |
| 95               | 112.026315789474 |
| 95.9736842105263 | 113              |
| 95               | 114.994252873563 |
| 95.9942528735632 | 114              |
| 95               | 115.015151515152 |
| 95.9848484848485 | 116              |
| 95.9893617021277 | 117.000000000000 |
| 95               | 118.982142857143 |
| 95.9821428571429 | 118              |
| 95               | 132.014705882353 |
| 95.9852941176471 | 133.000000000000 |
| 95.9954545454546 | 134              |
| 96               | 134.014285714286 |
| 96.9857142857143 | 135              |
| 97               | 135.022727272727 |
| 97.9772727272727 | 136              |
| 98               | 136.021739130435 |
| 98.9782608695652 | 137              |
| 99               | 137.038461538462 |
| 99.9615384615385 | 138.000000000000 |
| 100              | 138.022727272727 |
| 100.977272727273 | 139              |
| 101              | 139.041666666667 |
+------------------+------------------+
1
What are x and y here?Chris Taylor
Please include the original x and y so that we can reconstruct the problem on our end. Also, you may not be using interp1 correctly. The way you are calling it is such that it assumes your data in the first parameter goes from 1 to N where N is the length of your signal. The second parameter are the values you wish to interpolate.... so I don't think you're using it correctly. Can you include more code or how you're exactly creating that plot?rayryeng
I'll attached the original x and y data in a second. The interpolated data (blue line) is more or less what I would like to generated, but around the original data. Essentially, I am trying to remove the large "humps" in the data, to smooth it out. I am creating that plot exactly as above (will correct a minor in a second). So, I have x and y coordinates from Vertices(:,2) and Vertices(:,1) respectively. I then run the interp1 command and try plotting the "new y2" against the old x.m.n

1 Answers

1
votes

There are few problems with your code.
First, I think you used the interp1 function incorrectly. As you used it, the function interpolates x at the points specified by y assuming x is sampled at 1,2,3,....
Check out interp1 documentation.
What I think you tried to do is interpolate y at 1,2,3,...
If I am correct then the syntax should be (but it wouldn't work as I explain later)

x2 = 1:max(round(x));
y2 = interp1(x, y,x2 , 'nearest');
plot(x2,y2,'x');

Which brings me to the other two problems.
Your grid points are not monotonically increased and not unique.
The first problem can be solved by using sort:

[x,ind] = sort(x);
y = y(ind);

The second one can be solved with unique (however this is not necessarily how you would want to do that):

[x,ind] = unique(x);
y = y(ind);

Finally, you can interpolate as I suggested above.

BTW: there are other ways to "remove the large humps in the data" or "to smooth it out", such as filtering (linear and non-linear).