why do I get a strange result with griddata cubic?

Hi! I used the griddata cubic interpolation for the values of an image. I get this image that has a much higher range, from -300 to 400 aproximately. The non processed image has a range from 90 to 130. Any ideas why this happens?Thanks!

3 Comments

Any ideas why this happens?
Not without the original image and the code that transformed it.
u=[-20:0.05 :20];%5 cm
v=[0:-0.05:-20];
[Xq,Yq] = meshgrid(u,v);
Zq=zeros(length(v),length(u));
figure(30)
mesh(Xq,Yq,Zq)
view(2)
% Interpolazione con griddata: CUBIC
vq_4 = griddata(x,y,C,xq,yq,'cubic');
Unrecognized function or variable 'x'.
V_4=zeros(length(v),length(u));
for i=1:1:length(v)
V_4(i,:)=vq_4(1+(i-1)*801:1+(i-1)*801+800,1)';
end
%% PLOT
figure(34)
surf(Xq,Yq,Zq,V_4,'EdgeColor','none')
title('INTERPOLAZIONE CUBIC RISOLUZIONE 5 cm')
colormap gray
colorbar
axis equal
xlabel('est')
ylabel('nord')
% view(2)
The code you've posted does not run, I'm afraid:
Unrecognized function or variable 'x'.
Error in test (line 12)
vq_4 = griddata(x,y,C,xq,yq,'cubic');

Sign in to comment.

 Accepted Answer

This is expected behavior when there are sharp edges, especially with a surrounding flat area.
B--
|
-A-
Especially if the region near B is dense, but the edge itself is sparse, then the polynomial generated near the edge needs to mathematically dip down below the base in order to gain the necessary steepness to match the flat control points to the left together with the high points to the right. Polynomials cannot just suddenly rise steeply without a leading edge, so the math needs to insert a downward edge to be able to rise from.

9 Comments

Sorry, I am not sure to have understood. Could you try to explain it again? What do edges refer to? Many Thanks
Your image is grayscale, so each location is a grayscale intensity.
Take a horizontal cross-section across your image. Moving left to right, you have dull gray, brighter color, narrow black, a bit brighter, then a bit duller.
Now imagine plotting that cross-section line with position on the x axes, and intensity on the y axes. Low, high, sudden very low, up to moderate again soon after, then medium.
----
-------/ \--+ +-----
| |
v
So on the cross-section graph of intensity, there are edges.
And what you are finding is that near that sharp edge where it goes black, that you are getting interpolated values that are negative. This is not unexpected.
You are using cubic fit, so at each step there are four control values that a degree 3 polynomial is being fit to. Imagine you have some points
D
A B C
In order to fit a cubic to those four points, there has to be two reversal of direction, and the line has to exit on opposite sides of the baseline (if it starts from above it has to end below). In this case with D being above C, the most natural fit would be that the line goes up through C into D and exits above right, which would require that the line enters below left, going up through A, reversing direction between A and B, going down through B, reversing direction between B and C, and going up through C into D. Notice that this implied a minima between B and C -- a point below the baseline.
Now as you bring B and C closer together, the dip and rise has to get steeper and steeper. If your control points are close enough, you can end up with an arbitrarily low minima between B and C that is mathematically required to fit a cubic and yet is not present in any of the data.
/ \ /D
A B C
/ \ /
Likewise as you bring A and B together, you can end up with an implied peak that is higher than any of the datapoints you have.
Thank you very much! Much better now! Infact, if I create fewer points in the grid where I want the interpolated values, the image is better. So if the grid is dense the nearest neighbour method, for example, is better, right?
Nearest neighbor would not have this problem, but also will not give a smooth curve.
The linear method also never overshoot.
For spline method sometime rescaling x/y (or apply some linear transformation) might also reduce overshoot. I believe I have answered similar question somewhere here.
Hi @Bruno Luong what do you mean by rescaling x/y?
working with xs/ys instead of x/y
xs = sx*x
ys = sy*y
sx, sy are appropriate scaling factors that you have to figure it out.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!