python - Transforming and Resampling a 3D volume with numpy/scipy -


update:

i created documented ipython notebook. if want code, @ first answer.

question

i've got 40x40x40 volume of greyscale values. needs rotated/shifted/sheared.

here useful collection of homogeneous transformations: http://www.lfd.uci.edu/~gohlke/code/transformations.py.html

i need treat every voxel in volume pair of (position vector, value). transform position , sample new values each coordinate set of transformed vectors.

the sampling seems rather difficult, , glad find this: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.ndimage.affine_transform.html#scipy.ndimage.affine_transform

the given matrix , offset used find each point in output corresponding coordinates in input affine transformation. value of input @ coordinates determined spline interpolation of requested order. points outside boundaries of input filled according given mode.

sounds perfect.

but usage tricky. here using code rotating image. rotation matrix 2x2, that's not in homogenous coordinates. tried passing translation matrix in homogenous coordinates (2d) function:

dim =10 arr=np.zeros((dim,dim)) arr[0,0]=1 mat=np.array([[1,0,1],[0,1,0],[0,0,1]]) out3=scipy.ndimage.affine_transform(arr,mat) print("out3: ",out3) 

which produces error:

traceback (most recent call last):   file "c:/users/212590884/pycharmprojects/3daugmentation/main.py", line 32, in <module>     out3=scipy.ndimage.affine_transform(arr,mat)   file "c:\users\212590884\appdata\local\continuum\anaconda2\lib\site-packages\scipy\ndimage\interpolation.py", line 417, in affine_transform     raise runtimeerror('affine matrix has wrong number of rows') runtimeerror: affine matrix has wrong number of rows 

apparently doesn't work homogeneous coordinates. how can use shift data?

and in 2d, in 3d can't rotate volume:

dim =10 arr=np.zeros((dim,dim,dim)) arr[0,0]=1  angle=10/180*np.pi c=np.cos(angle) s=np.sin(angle) mat=np.array([[c,-s,0,0],[s,c,0,0],[0,0,1,0],[0,0,0,1]]) out3=scipy.ndimage.affine_transform(arr,mat) print("out3: ",out3) 

the error message same: affine matrix has wrong number of rows

is possible use method transform volume ?

i found collection of helper methods, offer shift , rotate not shear: https://docs.scipy.org/doc/scipy-0.14.0/reference/ndimage.html

but prefer use custom transformation matrix.

i've found option: map_coordinates

with numpy possible generate meshgrid of coordinates, reshape/stack them form position vectors. these vectors transformed , converted meshgrid coordinate format. map_coordinates sampling problem solved.

i think common problem , have created ipython notebook explains step step:

http://nbviewer.jupyter.org/gist/lhk/f05ee20b5a826e4c8b9bb3e528348688

there still 1 problem: order of coordinates strange. need reorder meshgrids in unintuitive way. bug in code.

please aware reordering of coordinates influences axes of transformations. if want rotate around x axis, corresponding vector not (1,0,0) (0,1,0), it's strange.

but works, , think principle clear.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -