Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DmitryUlyanov
Created May 8, 2017 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitryUlyanov/d82c50f7e2e0d778340852a0211150cb to your computer and use it in GitHub Desktop.
Save DmitryUlyanov/d82c50f7e2e0d778340852a0211150cb to your computer and use it in GitHub Desktop.
import numba
def FindLowAndHighIndices(x, m):
xf = np.floor(x)
l = np.clip(xf, 0, m-1).astype(int)
h = np.clip(xf + 1, 0, m-1).astype(int)
return l, h
@numba.autojit
def TriLinearInterpolate(xa, ya, za, V):
x0, x1 = FindLowAndHighIndices(xa, V.shape[0])
y0, y1 = FindLowAndHighIndices(ya, V.shape[1])
z0, z1 = FindLowAndHighIndices(za, V.shape[2])
xDif, yDif, zDif = xa - x0, ya - y0, za - z0
xd = (xa - x0) / (x1 - x0)
yd = (ya - y0) / (y1 - y0)
zd = (za - z0) / (z1 - z0)
out = np.zeros([xa.shape[0], ya.shape[0],za.shape[0]])
for ix, x in enumerate(xa):
for iy, y in enumerate(ya):
for iz, z in enumerate(za):
if x1[ix] - x0[ix] == 0:
xd[ix] = 1
if y1[iy] - y0[iy] == 0:
yd[iy] = 1
if z1[iz] - z0[iz] == 0:
zd[iz] = 1
c00 = V[x0[ix], y0[iy], z0[iz]] * (1 - xd[ix])+ V[x1[ix], y0[iy], z0[iz]] * xd[ix]
c01 = V[x0[ix], y0[iy], z1[iz]] * (1 - xd[ix])+ V[x1[ix], y0[iy], z1[iz]] * xd[ix]
c10 = V[x0[ix], y1[iy], z0[iz]] * (1 - xd[ix])+ V[x1[ix], y1[iy], z0[iz]] * xd[ix]
c11 = V[x0[ix], y1[iy], z1[iz]] * (1 - xd[ix])+ V[x1[ix], y1[iy], z1[iz]] * xd[ix]
c0 = c00 * (1 - yd[iy]) + c10 * yd[iy]
c1 = c01 * (1 - yd[iy]) + c11 * yd[iy]
c = c0 * (1 - zd[iz]) + c1 * zd[iz]
out[ix,iy,iz] = c
return out
x = np.linspace(0, 600-1, 600//4)
y = np.linspace(0, 600-1, 600//4)
z = np.linspace(0, 3-1, 3)
im_pil = Image.open("/home/dulyanov/CROPPED-Lempitsky and Ulyanov 3.jpg")
im = np.array(im_pil)
im2 = TriLinearInterpolate(x,y,z, im)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment