0

i want to extract an interpolated straight line between two arbitrary points from a equidistant 3D matrix, such as:

data_points_on_line=extract_line_from_3D_matrix(matrix,point1,point2,number_of_data_values_needed)

with the 3D matrix matrix, start and end point point1,point2 and the resolution of returned data between point1 and point2 given by an integer with number_of_data_values_needed.

i know that it's pretty simple when extracting data from a 2d array with scipy.ndimage but does anybody know whether there's already a function in scipy or numpy which can do that in 3D?

many thanks in advance!

Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42
rennau
  • 11
  • 3
  • 1
    sounds like scipy.interpolate.LinearNDInterpolator could be of help. Can you show what exactly you would do in the 2d case and why it doesn't work for 3d? This way it should be easier to understand your problem – dnalow Sep 12 '16 at 09:46
  • ok. i'll check that function. in 2d i would do it this way: [link](http://stackoverflow.com/questions/7878398/how-to-extract-an-arbitrary-line-of-values-from-a-numpy-array) but a point somewhere within the matrix basically depends on data values in three spatial dimensions. – rennau Sep 12 '16 at 12:05

1 Answers1

1

it works for me using InterpolatingFunction, for example:

#!/usr/bin/python
import numpy as np

from Scientific.Functions.Interpolation import InterpolatingFunction

def linear_interp_3d(point1,point2,spacing):
    xi=np.linspace(point1[0],point2[0],spacing)
    yi=np.linspace(point1[1],point2[1],spacing)
    zi=np.linspace(point1[2],point2[2],spacing)

    return xi,yi,zi

n=4
x = np.arange(0, n, 1)
y = np.arange(0, n, 1)
z = np.arange(0, n, 1)

data = np.ones(shape = (n, n, n))
for dummy in range(n):
    data[dummy,:,:]=dummy**3
axes = (x,y,z)
f = InterpolatingFunction(axes, data)

point1=(0,0,0)
point2=(3,3,3)
linevals=linear_interp_3d(point1,point2,11)

line=[]
for dummy in range(len(linevals[0][:])):
    line.append(f(linevals[0][dummy],linevals[1][dummy],linevals[2][dummy]))
print line
rennau
  • 11
  • 3