0

I know there has already been a lot of questioning about spline extrapolation in Matlab. I have an example where this works great in 2D and I want to understand this behaviour to programm it in C# using Math.Net. Here is my example:

matrix = zeros(128,128);
barWidth = 10;

%Calculate Midpoints
midpointX = floor(size(matrix,2)/2) + 1;
midpointY = floor(size(matrix,1)/2) + 1;

matrix(midpointY-barWidth:midpointY+barWidth,:) = 1;

% Windowing (I know it could be shortened...)
distanceMatY = -midpointY+1:midpointY-2;
distanceMatY = abs(repmat(distanceMatY',1,size(matrix,2)));
Factor = 0.5*(cos(pi*distanceMatY/barWidth)+1);
index = distanceMatY > barWidth;
Factor(index) = 0;
matrix = matrix.*Factor;

% Rotate matrix
alpha = 30 * pi/180;
y = -midpointY+1:midpointY-2; y = y'; 
x = -midpointX+1:midpointX-1;     %set new COS for rotation in Midpoint
xRot = x*cos(alpha) - y*sin(alpha) + nMP;  %Determine X and Y matrices for
yRot = x*sin(alpha) + y*cos(alpha) + mMP;  %Roatation with angle alpha

rotMatrix = interp2(matrix,xRot,yRot,'linear');      %Interpolate rotated matrix

I have a matrix of zeros with a bar over the whole length in the middle.
Input Matrix
I applied a Hann Window to the bar to smooth the edges and then rotated the matrix by 30 degrees with bilinear interpolation.
Rotated Matrix bilinear
Now I have values outside the boundaries which are set to NaN. I could set all the NaN values to zero but what I really want is that the bar will be extended automatically. Now I could pad the matrix before rotation and cut it again after rotation to the input matrix size. Much easier it is to just use interp2(matrix,xRot,yRot,'spline') and the rotated matrix looks exactly as I want for each angle.
Rotated Matrix spline
How does the 2D spline interpolation does it and is there a way to program that manually?

Till
  • 183
  • 7
  • This page explains it in detail: https://en.m.wikipedia.org/wiki/Spline_interpolation — also, there are links to implementations at the bottom. – Cris Luengo Nov 29 '21 at 15:12
  • @CrisLuengo Thanks for the link. It helps to understand why the values stay the same at the end of the bar and extrapolation works. If I understand it correctly - to programm the 2D-Interpolation I need to assemble and solve a equation system. Let's say I already have 1D-Splines for each row and column of a matrix. Could I also directly use these splines to interpolate for any x-/y-Value of this matrix? – Till Nov 30 '21 at 12:19
  • 2D interpolation usually works by first interpolating each row to the new grid, then in the result, interpolate each column to the new grid. I don’t know if there is a shortcut that would allow you to use splines for each row and column of the input image to directly obtain the interpolated value anywhere. I’ve always preferred using a [cubic spline kernel for interpolation](https://en.m.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm) (where you only need 16 input values in 2D to find one interpolated value). – Cris Luengo Nov 30 '21 at 14:35

0 Answers0