Skip to content

Commit

Permalink
first draft - not working
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudbore committed Jul 25, 2024
1 parent ce558fd commit 3b1880b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion scilpy/surfaces/surface_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import trimeshpy.vtk_util as vtk_u


def apply_transform(mesh, ants_affine, ants_warp=None):
def apply_transform(mesh, ants_affine, ants_warp=None, inverse=False):
"""
Apply transformation to a surface
- Apply linear transformation with affine
Expand All @@ -27,6 +27,8 @@ def apply_transform(mesh, ants_affine, ants_warp=None):
"""
# Affine transformation
inv_affine = np.linalg.inv(ants_affine)
if inverse:
linear_transfo = np.linalg.inv(linear_transfo)

# Transform mesh vertices
mesh.set_vertices(mesh.vertices_affine(inv_affine))
Expand Down
38 changes: 21 additions & 17 deletions scripts/scil_surface_apply_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Example usage from T1 to b0 using ANTs transforms:
> ConvertTransformFile 3 output0GenericAffine.mat vtk_transfo.txt --hm
> scil_surface_apply_transform.py lh_white_lps.vtk affine.txt lh_white_b0.vtk\\
--ants_warp warp.nii.gz
--in_deformation warp.nii.gz
Important: The input surface needs to be in *T1 world LPS* coordinates
(aligned over the T1 in MI-Brain).
Expand All @@ -30,7 +30,8 @@
from scilpy.io.utils import (add_overwrite_arg,
add_verbose_arg,
assert_inputs_exist,
assert_outputs_exist)
assert_outputs_exist,
load_matrix_in_any_format)
from scilpy.surfaces.surface_operations import apply_transform


Expand All @@ -45,17 +46,20 @@ def _build_arg_parser():
p = argparse.ArgumentParser(description=__doc__, epilog=EPILOG,
formatter_class=argparse.RawTextHelpFormatter)

p.add_argument('in_surface',
p.add_argument('in_moving_surface',
help='Input surface (.vtk).')

p.add_argument('ants_affine',
help='Affine transform from ANTs (.txt or .mat).')

p.add_argument('in_transfo',
help='Path of the file containing the 4x4 \n'
'transformation, matrix (.txt, .npy or .mat).'))
p.add_argument('out_surface',
help='Output surface (.vtk).')

p.add_argument('--ants_warp',
help='Warp image from ANTs (Nifti image).')
g = p.add_argument_group("Transformation options")
g.add_argument('--inverse', action='store_true',
help='Apply the inverse linear transformation.')
g.add_argument('--in_deformation', metavar='file',
help='Path to the file containing a deformation field.')

add_verbose_arg(p)
add_overwrite_arg(p)
Expand All @@ -68,23 +72,23 @@ def main():
args = parser.parse_args()
logging.getLogger().setLevel(logging.getLevelName(args.verbose))

assert_inputs_exist(parser, [args.in_surface, args.ants_affine],
assert_inputs_exist(parser, [args.in_surface, args.in_transfo],
args.ants_warp)
assert_outputs_exist(parser, args, args.out_surface)

# Load mesh
mesh = load_mesh_from_file(args.in_surface)

warp_img = None
# Load affine
affine = np.loadtxt(args.ants_affine)
# Load transformation
transfo = load_matrix_in_any_format(args.in_transfo)

# Non linear transformation
if args.ants_warp:
# Load warp
warp_img = nib.load(args.ants_warp)
deformation_data = None
if args.in_deformation is not None:
deformation_data = np.squeeze(nib.load(
args.in_deformation).get_fdata(dtype=np.float32))

mesh = apply_transform(mesh, affine, warp_img)
mesh = apply_transform(mesh, transfo, deformation_data,
inverse=args.inverse)

# Save mesh
mesh.save(args.out_surface)
Expand Down

0 comments on commit 3b1880b

Please sign in to comment.