The Problem
I am currently writing a script that converts images into numerical array representation and then calculates "in-between" images based on linear interpolation between the start and end array.
My code does exactly what I want but goes over many nested loops which strikes me as something that will lead to very high computation times for many interpolation steps or big images.
The Code
The code is in python
import numpy as np
# Helper function that calculates the interpolation between two points
def interpolate_points(p1, p2, n_steps=3):
# interpolate ratios between the points
ratios = np.linspace(0, 1, num=n_steps)
# linear interpolate vectors
vectors = list()
for ratio in ratios:
v = (1.0 - ratio) * p1 + ratio * p2
vectors.append(v)
return np.asarray(vectors)
# final function that interpolates arrays
def interpolate_arrays(start_array,end_array,n_steps=10):
n = 0
array_interpolation = []
while n < n_steps:
i = 0
x = []
while i < len(start_array):
e = interpolate_points(start_array[i],end_array[i],n_steps)[n]
x.append(e)
i += 1
array_interpolation += [x]
n += 1
return array_interpolation
This results in:
#Test
X1 = [1,1]
X2 = [3,3]
interpolate_arrays(X1,X2,n_steps=3)
#[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]