registration.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from nipype.interfaces import ants
  2. import warnings
  3. warnings.simplefilter(action='ignore', category=FutureWarning)
  4. # Parameters for a full registration (Affine,Syn) --- SLOW ---
  5. def full_registration(fixed, moving, output='full_trans.nii', transform_pre=''):
  6. ants.Registration(
  7. dimension=2,
  8. transforms=['Rigid', 'Affine', 'SyN'],
  9. transform_parameters=[(0.15,), (0.15,), (0.3, 3, 0.5)],
  10. metric=['CC', 'MI'] * 3,
  11. convergence_threshold=[1.e-7],
  12. number_of_iterations=[[1000, 1000, 1000, 1000], [1000, 1000, 1000, 1000],
  13. [1000, 1000, 1000, 1000]],
  14. smoothing_sigmas=[[4, 4, 2, 2], [4, 4, 2, 2], [4, 2, 2, 1]],
  15. shrink_factors=[[32, 16, 8, 4], [32, 16, 8, 4], [16, 8, 4, 2]],
  16. initial_moving_transform_com=1,
  17. interpolation='BSpline',
  18. metric_weight=[0.5] * 6,
  19. radius_or_number_of_bins=[4, 32] * 3,
  20. sampling_strategy=['Regular'] * 3,
  21. use_histogram_matching=True,
  22. winsorize_lower_quantile=0.05,
  23. winsorize_upper_quantile=0.95,
  24. collapse_output_transforms=True,
  25. output_transform_prefix=transform_pre,
  26. fixed_image=[fixed],
  27. moving_image=[moving],
  28. output_warped_image=output,
  29. num_threads=12,
  30. verbose=False,
  31. write_composite_transform=True,
  32. output_inverse_warped_image=True
  33. ).run()
  34. return output
  35. # A faster rigid registration
  36. def rigid_registration(fixed, moving, output='rigid_trans.nii', transform_pre=''):
  37. ants.Registration(
  38. dimension=2,
  39. transforms=['Rigid'],
  40. transform_parameters=[(0.15,)],
  41. metric=['MI'],
  42. smoothing_sigmas=[[4, 4, 2]],
  43. convergence_threshold=[1.e-6],
  44. number_of_iterations=[[1000, 1000, 1000]],
  45. shrink_factors=[[32, 16, 8]],
  46. initial_moving_transform_com=1,
  47. interpolation='BSpline',
  48. metric_weight=[1],
  49. radius_or_number_of_bins=[32],
  50. sampling_strategy=['Regular'],
  51. use_histogram_matching=True,
  52. winsorize_lower_quantile=0.05,
  53. winsorize_upper_quantile=0.95,
  54. terminal_output='none',
  55. output_transform_prefix=transform_pre,
  56. fixed_image=[fixed],
  57. moving_image=[moving],
  58. output_warped_image=output
  59. ).run()
  60. return output