# Bilinear Interpolation¶

This example shows how to use the pylops.signalprocessing.Bilinar operator to perform bilinear interpolation to a 2-dimensional input vector.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as pltgs
from scipy import misc

import pylops

plt.close('all')
np.random.seed(0)


First of all, we create a 2-dimensional input vector containing an image from the scipy.misc family.

x = misc.face()[::5, ::5, 0]
nz, nx = x.shape


We can now define a set of available samples in the first and second direction of the array and apply bilinear interpolation.

nsamples = 2000
iava = np.vstack((np.random.uniform(0, nz-1, nsamples),
np.random.uniform(0, nx-1, nsamples)))

Bop = pylops.signalprocessing.Bilinear(iava, (nz, nx))
y = Bop * x.ravel()


At this point we try to reconstruct the input signal imposing a smooth solution by means of a regularization term that minimizes the Laplacian of the solution.

D2op = pylops.Laplacian((nz, nx), weights=(1, 1), dtype='float64')

xinv = pylops.optimization.leastsquares.NormalEquationsInversion(Bop, [D2op], y,
epsRs=[np.sqrt(0.1)],
returninfo=False,
**dict(maxiter=100))
xinv = xinv.reshape(nz, nx)

fig, axs = plt.subplots(1, 3, figsize=(10, 4))
fig.suptitle('Bilinear interpolation', fontsize=14,
fontweight='bold', y=0.95)
axs.imshow(x, cmap='gray_r', vmin=0, vmax=250)
axs.axis('tight')
axs.set_title('Original')
axs.axis('tight')
axs.set_title('Sampled')
axs.imshow(xinv, cmap='gray_r', vmin=0, vmax=250)
axs.axis('tight')
axs.set_title('2D Regularization')
plt.tight_layout() Out:

/home/docs/checkouts/readthedocs.org/user_builds/pylops/envs/latest/lib/python3.6/site-packages/pylops-1.14.1.dev2+g2aa7dc9-py3.6.egg/pylops/signalprocessing/Bilinear.py:123: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
/home/docs/checkouts/readthedocs.org/user_builds/pylops/envs/latest/lib/python3.6/site-packages/pylops-1.14.1.dev2+g2aa7dc9-py3.6.egg/pylops/signalprocessing/Bilinear.py:125: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
/home/docs/checkouts/readthedocs.org/user_builds/pylops/envs/latest/lib/python3.6/site-packages/pylops-1.14.1.dev2+g2aa7dc9-py3.6.egg/pylops/signalprocessing/Bilinear.py:127: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
/home/docs/checkouts/readthedocs.org/user_builds/pylops/envs/latest/lib/python3.6/site-packages/pylops-1.14.1.dev2+g2aa7dc9-py3.6.egg/pylops/signalprocessing/Bilinear.py:129: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.


Total running time of the script: ( 0 minutes 0.955 seconds)

Gallery generated by Sphinx-Gallery