Bilinear spline interpolation MATLAB using MESHGRID and SURF

Click For Summary
SUMMARY

The discussion focuses on implementing bilinear spline interpolation in MATLAB using a discrete N x N matrix representing a greyscale image. The user seeks assistance in coding a MATLAB script named bilinearSpline.m that utilizes the meshgrid and surf functions for visualization. The provided code snippet demonstrates how to define the interpolation function and visualize the results, clarifying the roles of input parameters such as A, xx, yy, xq, and yq. The user confirms that u_{ij} corresponds to the matrix values, while \Omega represents the Cartesian product as specified by the course requirements.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of bilinear interpolation concepts
  • Knowledge of matrix operations in MATLAB
  • Experience with MATLAB visualization functions such as meshgrid and surf
NEXT STEPS
  • Study the MATLAB documentation for meshgrid and surf functions
  • Learn about vectorization techniques in MATLAB to optimize performance
  • Explore advanced interpolation methods in MATLAB, such as bicubic interpolation
  • Investigate image processing techniques in MATLAB for further applications
USEFUL FOR

This discussion is beneficial for students in image processing courses, MATLAB developers working on interpolation techniques, and anyone interested in enhancing their skills in visualizing mathematical functions using MATLAB.

AVBs2Systems
Messages
39
Reaction score
24
TL;DR
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: 430
Last edited:
Physics news on Phys.org
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:
  • Like
Likes   Reactions: AVBs2Systems
kreil said:
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:

Similar threads

Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K