Bilinear spline interpolation MATLAB using MESHGRID and SURF

In summary, the student is trying to code a function that interpolates a picture in a domain. The program does most of the work for him, but he needs to check the details.
  • #1
AVBs2Systems
39
24
TL;DR Summary
Bilinear spline interpolation in matlab using a spline function. MESHGRID and SURF commands needed.
Hello.
So, I must provide a solution for an image processing course I am taking (implemented in MATLAB).The task is as follows:

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

  • 2019-11-08 00_44_28-ex01 (2).pdf.png
    2019-11-08 00_44_28-ex01 (2).pdf.png
    21 KB · Views: 345
Last edited:
Physics news on Phys.org
  • #2
Where does [itex]u_{ij}[/itex] come from? Should that be [itex]\Omega_{ij}[/itex]..?

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:
  • Like
Likes AVBs2Systems
  • #3
kreil said:
Where does [itex]u_{ij}[/itex] come from? Should that be [itex]\Omega_{ij}[/itex]..?

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:

1. What is bilinear spline interpolation in MATLAB?

Bilinear spline interpolation is a method used to interpolate values between known data points in a two-dimensional plane. It involves creating a smooth surface or "mesh" over the known data points and using this mesh to estimate the values at unknown points.

2. How does MESHGRID work in MATLAB?

MESHGRID is a function in MATLAB that takes two vectors as input and returns two matrices as output. These matrices represent a grid of points in a two-dimensional plane, which can be used for plotting or interpolation purposes.

3. What is the purpose of the SURF function in MATLAB?

The SURF function in MATLAB is used to create a surface plot of a two-dimensional dataset. It takes in the x, y, and z coordinates of the data points and creates a surface that connects these points, allowing for visual representation of the data.

4. How accurate is bilinear spline interpolation in MATLAB?

The accuracy of bilinear spline interpolation in MATLAB depends on the density and distribution of the known data points. In general, the more evenly spaced and dense the data points are, the more accurate the interpolation will be. However, it is always important to carefully analyze and validate the results of any interpolation method.

5. Can bilinear spline interpolation be used for extrapolation?

No, bilinear spline interpolation in MATLAB is not suitable for extrapolation, as it requires known data points within the range of the unknown points. Extrapolation involves estimating values outside of the known data range, which can lead to unreliable results.

Similar threads

  • Programming and Computer Science
Replies
3
Views
352
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
123
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
5
Views
384
  • Calculus and Beyond Homework Help
Replies
10
Views
986
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top