# Bilinear spline interpolation MATLAB using MESHGRID and SURF

• MATLAB

## Summary:

Bilinear spline interpolation in matlab using a spline function. MESHGRID and SURF commands needed.

## Main Question or Discussion Point

Hello.
So, I must provide a solution for an image processing course I am taking (implemented in MATLAB).

1. I must provide a MATLAB script that takes in a DISCRETE N x N matrix (Greyscale picture) and does Bilinear spline interpolation on it.
This is the spline function used in one dimension:

$$\textbf{Mathematical objects}$$
The Square matrix which represents the greyscale image:
$$\Omega = \{1,2,3.....N\} \times \{1,2,3.....N\}$$
The basic spline function for 1 D images (NOT for the matrix above!)
$$b(x) = \text{Max}( 0, 1 - |x|)$$
The above is equivalent to the triangle function in signal processing:
$$b(x) = \text{Tri}\bigg(\frac{x}{2} \bigg)$$
This was ok for interpolating simple sequences, or "1 dimensional" images.
Now, for 2dimensional images, the function gets modified, into big B (This is USED FOR $\Omega$ )
$$B(x,y) = \text{Max}( 0, 1 - |x|) \cdot \text{Max}( 0, 1 - |y|)$$
The result of the interpolation is this function:
$$u(x,y) = \displaystyle \sum_{i , j \in \Omega} u_{i,j} \cdot B_{i , j}(x,y) = \displaystyle \sum_{i,j \in \Omega} u_{i,j} \cdot \text{Max}( 0, 1 - |x - i|) \cdot \text{Max}( 0, 1 - |y - j|)$$
Now, the task is attached as the photo below:

Now, your image is given via the matrix $A = [ 69, 110, 196;... 50, 226, 101;... 210, 100, 206 ]$ Write a Matlab script bilinearSpline.m which visualizes the corresponding interpolating function u in the domain $[1, 3] ⊆ \mathbb{R}^{2}$ . Check help and documentation of the commands meshgrid and surf in order to visualize your function.

I am in desperate need of any help, how should I code this? and what would the result actually look like? I have some knowledge of matlab, but I am a little lost on how to implement it.

Here is what I understood:
1. Need two for loops for the array.
2. Need to define the spline function (No idea how to in matlab).
3. Need to use the meshgrid and surf command to create the plot in the 1,3 x 1,3 domain.

#### Attachments

• 35.2 KB Views: 49
Last edited:

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
kreil
Gold Member
Where does $u_{ij}$ come from? Should that be $\Omega_{ij}$..?

Assuming so, here is a program that does most of what you want, but that requires you to check the details:

Code:
function z = bilinearSpline(xx,yy,A,xq,yq)
% --xx and yy are the sample grid points
% --A is the image matrix, acting as values at the (xx,yy) locations
% --xq and yq are the query points where the interpolation is performed.

m = size(xq,1); % xq is also square
z = zeros(size(xq)); % preallocate output

for k = 1:m
for p = 1:m
% Pick query point
x = xq(k,p);
y = yq(k,p);
u = A.*max(0,1-abs(x-xx)).*max(0,1-abs(y-yy)); % vectorize the summation over the values in A
z(k,p) = sum(u,'all'); % Add all summation terms to find interpolated value
end
end

% Plot result on the grid of query points
surf(xq,yq,z)

return % return result for z
end
You would call the function with the grid A is defined on for (xx,yy), and the points where you want to interpolate (xq,yq), e.g.

Code:
A = [69 110 196; 50 226 101; 210 100 206];
[xx,yy] = meshgrid(1:3);
[xq,yq] = meshgrid(1:0.05:3);
z = bilinearSpline(xx,yy,A,xq,yq)

Last edited:
• AVBs2Systems
Where does $u_{ij}$ come from? Should that be $\Omega_{ij}$..?

Assuming so, here is a program that does most of what you want, but that requires you to check the details:

Code:
function z = bilinearSpline(xx,yy,A,xq,yq)
% --xx and yy are the sample grid points
% --A is the image matrix, acting as values at the (xx,yy) locations
% --xq and yq are the query points where the interpolation is performed.

m = size(xq,1); % xq is also square
z = zeros(size(xq)); % preallocate output

for k = 1:m
for p = 1:m
% Pick query point
x = xq(k,p);
y = yq(k,p);
u = A.*max(0,1-abs(x-xx)).*max(0,1-abs(y-yy)); % vectorize the summation over the values in A
z(k,p) = sum(u,'all'); % Add all summation terms to find interpolated value
end
end

% Plot result on the grid of query points
surf(xq,yq,z)

return % return result for z
end
You would call the function with the grid A is defined on for (xx,yy), and the points where you want to interpolate (xq,yq), e.g.

Code:
A = [69 110 196; 50 226 101; 210 100 206];
[xx,yy] = meshgrid(1:3);
[xq,yq] = meshgrid(1:0.05:3);
z = bilinearSpline(xx,yy,A,xq,yq)
My apologies,

$$u_{i , j}$$
Is the matrix, whereas $$\Omega$$
I sismply the cartesian product as the professor has given above.

I must say I am very grateful for the time you took to actually write the code, thank you. I will check the details and post the results in a few hours as I am at work now!

Last edited: