Plot 1D Lanczos Interpolation

433 views Asked by At

Imagine we have the following points:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 3, 5.5, 6.5, 5, 4, 5, 7.5, 8, 7]

enter image description here

Now we want to interpolate every point using lanczos interpolation like:

lanczos_interpolation = lanczos_interpolation(x, y)
xs = np.linspace(0, 10, 100)
interpolated_xs = lanczos_interpolation(xs)

How can I implement the lanczos_interpolation? I have already seen interpolate.interp1d but lanczos is not available...

I want something that if plotted would look like (the provided example is with bilinear interpolation): enter image description here

1

There are 1 answers

11
Colim On

This is the OpenCV Lanczos interpolation.

EDIT: Open CV interpolates for images, so actually it extrapolates data, because the starting points are pixels, and an image with more pixels, since it has smaller pixels, it has pixels outside the original range:

For example, on this image, initially there are 2 pixels, but if OpenCV is asked to interpolate to 20 smaller pixels, there would be 5 extra pixels on each side of the image. That is why the interpolation doesn't match. enter image description here

Unfortunately, this means that this method cannot interpolate between points at arbitrary coordinates. The original points have to have a constant separation.

This code does a dirty approximation, because there is no guarantee that the pixels will match exactly:


import numpy as np
import matplotlib.pyplot as plt
import cv2

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 3, 5.5, 6.5, 5, 4, 5, 7.5, 8, 7]

img = np.array([y])
parts = 100
xs = np.linspace(1, 10, parts)
resized = cv2.resize(img, (int(parts*(1+1/len(y))), 1),
                     interpolation=cv2.INTER_LANCZOS4)[0]

plt.scatter(x, y)
plt.plot(xs, resized[int(parts/(2*len(y))):int(parts/(2*len(y)))+parts])
plt.show()

enter image description here