'''
Created on 24 avr. 2018
@author: Colley Jean-Marc, APC/IN2P3/CNRS
'''
from scipy.interpolate import RectBivariateSpline
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import splev, splrep
[docs]class InterpolInversMonotoneFunction(object):
def __init__(self, a_x, a_f_x, degree_spline=1):
# do spline, and inverse x , y
self.x = a_x
self.y = a_f_x
assert a_x.size == a_f_x.size
self.spl = splrep(a_f_x, a_x, k=degree_spline)
print(self.spl[0])
print(self.spl[1])
print(self.spl[2])
if self.spl[2] > 0:
print("ERROR: ",self.spl[2])
[docs] def plot(self):
plt.figure("MonotoneFunction ?")
plt.plot(self.x, self.y)
plt.plot(self.y, self.x)
plt.grid()
[docs] def __call__(self, a_y):
# return array solution
return splev(a_y, self.spl)
[docs]class InterpolLin2DCenter(object):
"""
https://scipython.com/book/chapter-8-scipy/examples/two-dimensional-interpolation-with-scipyinterpolaterectbivariatespline/
input : image and vector translation in pixel unit
output : interpol image only in domain center after translation
"""
def __init__(self, p_ima, p_ox, p_oy):
# coherence test
if (p_ima.shape[0]%2 == 1) :
raise
if (p_ox%2 == 1) or (p_oy%2 == 1):
raise
if (p_ox > p_ima.shape[0]) or (p_oy > p_ima.shape[1]):
raise
self.sox = p_ox
self.soy = p_oy
self.vmin = np.min(p_ima)
self.vmax = np.max(p_ima)
# 0 padding to obtain 0 out of domain with interpol method
ima_0 = np.zeros((p_ima.shape[0] + 2, p_ima.shape[1] + 2), dtype=np.float32)
ima_0[1:-1,1:-1] = np.array(p_ima,dtype=np.float32)
# plan interpol
ix = np.arange(0, ima_0.shape[0], dtype=np.float32) + 0.5
iy = np.arange(0, ima_0.shape[1], dtype=np.float32) + 0.5
self.o_interpol = RectBivariateSpline(ix, iy, ima_0, kx=1, ky=1, s=0)
# create center array
self.aix = np.arange(0, p_ox, dtype=np.float32) + (ima_0.shape[0] - p_ox)/2 + 0.5
self.aiy = np.arange(0, p_oy, dtype=np.float32) + (ima_0.shape[1] - p_oy)/2 + 0.5
[docs] def interpol(self, p_dx, p_dy):
# add translate to center array
print("offset: ", p_dx, p_dy)
aixd = self.aix + p_dx
aiyd = self.aiy + p_dy
# interpol
aimx, aimy = np.meshgrid(aixd, aiyd, indexing='ij')
out_inter = self.o_interpol.ev(aimx, aimy)
self.last = [aimx, aimy, out_inter]
return out_inter
[docs] def plot_interpol(self, p_title=""):
plt.figure()
plt.title(p_title)
plt.imshow(self.last[2], vmin=self.vmin, vmax=self.vmax)
#plt.imshow(self.last[2] , origin='lower')
plt.grid(linewidth=1)
plt.colorbar()