[5d12a0]: / ants / registration / create_warped_grid.py

Download this file

106 lines (83 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
__all__ = ['create_warped_grid']
import numpy as np
import ants
def create_warped_grid(image, grid_step=10, grid_width=2, grid_directions=(True, True),
fixed_reference_image=None, transform=None, foreground=1, background=0):
"""
Deforming a grid is a helpful way to visualize a deformation field.
This function enables a user to define the grid parameters
and apply a deformable map to that grid.
ANTsR function: `createWarpedGrid`
Arguments
---------
image : ANTsImage
input image
grid_step : scalar
width of grid blocks
grid_width : scalar
width of grid lines
grid_directions : tuple of booleans
directions in which to draw grid lines, boolean vector
fixed_reference_image : ANTsImage (optional)
reference image space
transform : list/tuple of strings (optional)
vector of transforms
foreground : scalar
intensity value for grid blocks
background : scalar
intensity value for grid lines
Returns
-------
ANTsImage
Example
-------
>>> import ants
>>> fi = ants.image_read( ants.get_ants_data( 'r16' ) )
>>> mi = ants.image_read( ants.get_ants_data( 'r64' ) )
>>> mygr = ants.create_warped_grid( mi )
>>> mytx = ants.registration(fixed=fi, moving=mi, type_of_transform = ('SyN') )
>>> mywarpedgrid = ants.create_warped_grid( mygr, grid_directions=(False,True),
transform=mytx['fwdtransforms'], fixed_reference_image=fi )
"""
if ants.is_image(image):
if len(grid_directions) != image.dimension:
grid_directions = [True]*image.dimension
garr = image.numpy() * 0 + foreground
else:
if not isinstance(image, (list, tuple)):
raise ValueError('image arg must be ANTsImage or list or tuple')
if len(grid_directions) != len(image):
grid_directions = [True]*len(image)
garr = np.zeros(image) + foreground
image = ants.from_numpy(garr)
idim = garr.ndim
gridw = grid_width
for d in range(idim):
togrid = np.arange(-1, garr.shape[d]-1, step=grid_step)
for i in range(len(togrid)):
if (d == 0) & (idim == 3) & (grid_directions[d]):
garr[togrid[i]:(togrid[i]+gridw),...] = background
garr[0,...] = background
garr[-1,...] = background
if (d == 1) & (idim == 3) & (grid_directions[d]):
garr[:,togrid[i]:(togrid[i]+gridw),:] = background
garr[:,0,:] = background
garr[:,-1,:] = background
if (d == 2) & (idim == 3) & (grid_directions[d]):
garr[...,togrid[i]:(togrid[i]+gridw)] = background
garr[...,0] = background
garr[...,-1] = background
if (d == 0) & (idim == 2) & (grid_directions[d]):
garr[togrid[i]:(togrid[i]+gridw),:] = background
garr[0,:] = background
garr[-1,:] = background
if (d == 1) & (idim == 2) & (grid_directions[d]):
garr[:,togrid[i]:(togrid[i]+gridw)] = background
garr[:,0] = background
garr[:,-1] = background
gimage = image.new_image_like(garr)
if (transform is not None) and (fixed_reference_image is not None):
return ants.apply_transforms( fixed=fixed_reference_image, moving=gimage,
transformlist=transform )
else:
return gimage