2D Interpolation Using MATLAB

Ammar Ali Nov 29, 2021
2D Interpolation Using MATLAB

This tutorial will discuss finding the interpolation of 2D data using the interp2() function in MATLAB.

Find the Interpolation of 2D Data Using the interp2() Function in MATLAB

We can use MATLAB’s built-in function interp2() function to find the interpolation of 2D gridded data in a mesh grid format. For example, let’s interpolate over a grid using the default method.

See the code below.

[x,y] = meshgrid(-3:3);
v = peaks(x,y);
title('Original Sampling');
[xq,yq] = meshgrid(-3:0.25:3);
vq = interp2(x,y,v,xq,yq);
title('Linear Interpolation Using Finer Grid');


interpolation using default method

In the output, the image on the right is the result of interpolation using the default method.

As you can see, the output image has more data points as compared with the original one. The x and y vector is the input matrices, and they should have the same size.

The v vector contains the sample values. It can contain real or complex values if v contains complex numbers.

Then, the interp2() function will interpolate the real and imaginary parts separately. The vector xq and yq contain the query points, which can be real scalars, vectors, matrices, or arrays.

We can also change the interpolation method by defining the new method as a string in the interp2() function like we can set cubic, nearest, or spline method for interpolation. We can use interpolation to refine a grayscale image.

If an image has fewer pixels, the details inside the image will not be visible. In this case, we can use interpolation to increase the number of pixels to understand the image.

For example, let’s load a grayscale image into MATLAB and refine it using the interp2() function. See the code below.

load flujet.mat
colormap gray
v = single(X(200:300,1:25));
axis off
title('Original Image')
vq = interp2(v,5);
axis off
title('Linear Interpolation')


refining image using interpolation

In the output, the right image is refined using linear interpolation. The function single() is used to convert the values to single precision.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Vector