-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Obtain Displacement Field at Original Input dimension #21
Comments
Hi! Are you looking for the displacement field at original resolution with displacements in physical coordinates? If so, this is doable by converting the itkCompositeTransform to a displacement field as follows: https://colab.research.google.com/drive/1bo_CWdI4PC7YdMmlVb2jYp0Fd1ee5gW_?usp=sharing !pip install unigradicon
!wget https://www.hgreer.com/assets/slicer_mirror/RegLib_C01_1.nrrd
!wget https://www.hgreer.com/assets/slicer_mirror/RegLib_C01_2.nrrd
!unigradicon-register --fixed=RegLib_C01_2.nrrd --fixed_modality=mri --moving=RegLib_C01_1.nrrd --moving_modality=mri --transform_out=trans.hdf5 --warped_moving_out=warped_C01_1.nrrd
import itk
fixed_image = itk.imread("RegLib_C01_2.nrrd")
moving_image = itk.imread("RegLib_C01_1.nrrd")
transform = itk.transformread("trans.hdf5")[0]
dispfield_filter = itk.TransformToDisplacementFieldFilter[itk.Image[itk.Vector[itk.F, 3], 3], itk.D].New()
dispfield_filter.SetTransform(transform)
dispfield_filter.SetReferenceImage(fixed_image)
dispfield_filter.SetUseReferenceImage(True)
dispfield_filter.Update()
displacement_field = dispfield_filter.GetOutput()
displacement_field.GetLargestPossibleRegion().GetSize()
print(np.array(displacement_field).shape)
warped_moving_image = itk.warp_image_filter(
moving_image,
output_origin=fixed_image.GetOrigin(),
output_direction=fixed_image.GetDirection(),
output_spacing=fixed_image.GetSpacing(),
displacement_field=displacement_field)
plt.imshow(itk.checker_board_image_filter(fixed_image, warped_moving_image)[50]) Does this work for your usecase? |
Could you provide the definitions of the functions get_dvf and warp_image? My suspicion is that somewhere in the pipeline the image metadata (spacing, orientation, and origin) is getting lost. Also, could you provide the output of the following script for the fixed image, the fixed label, the moving image, and the moving label? This will help me understand the image metadata. I'm sorry that this is taking so much effort to clear up!
|
Hello no issues at all , I am just glad and thankful for your help and guidance. Here is the code you need: def get_dvf(fixed,moving,save_dvf=None,transform_out=None,fixed_segmentation=None,moving_segmentation=None,io_iterations="None",moving_modality='mri',fixed_modality='mri'):
''' fixed : Path of the fixed image
moving : Path of moving image
save_dvf : True if want to save dvf as an hdf5 file
transform_out : Path of the hdf5 file
fixed_segmentation : Path of segmentation map of fixed Image
moving_segentation : Path of segmentation map of moving Image
io_iterations : Default none, number of iterations.
moving_modality : 'ct' or 'mri'
fixed_modality : 'ct' or 'mri'
'''
net = get_unigradicon()
fixed = itk.imread(fixed)
moving = itk.imread(moving)
if fixed_segmentation is not None:
fixed_segmentation = itk.imread(fixed_segmentation)
else:
fixed_segmentation = None
if moving_segmentation is not None:
moving_segmentation = itk.imread(moving_segmentation)
else:
moving_segmentation = None
if io_iterations == "None":
io_iterations = None
else:
io_iterations = int(io_iterations)
phi_AB, phi_BA = icon_registration.itk_wrapper.register_pair(
net,
preprocess(moving, moving_modality, moving_segmentation),
preprocess(fixed, fixed_modality, fixed_segmentation),
finetune_steps=io_iterations)
if save_dvf is not None:
if transform_out is None:
transform_out="trans.hdf5"
itk.transformwrite([phi_AB], transform_out)
return phi_AB,phi_BA,net
def warp_image(moving,fixed,phi_AB=None,transform=None,interpolator=None,save_img=None,warped_moving_out=None):
'''
fixed : Path of the fixed image
moving : Path of moving image
phi_AB : Phi returned from get_dvf()
interpolator : Linear or nearest_neighbor
save_img : If want o save image
warped_moving_out : Path of the image to be saved.
transform : path of hdf5 saved transform
'''
fixed = itk.imread(fixed)
moving = itk.imread(moving)
if interpolator=="linear" or interpolator is None:
interpolator = itk.LinearInterpolateImageFunction.New(moving)
elif interpolator=="nearest_neighbor":
interpolator = itk.NearestNeighborInterpolateImageFunction.New(moving)
else:
raise Exception("Specify --nearest_neighbor or --linear")
if transform is not None and phi_AB is None:
phi_AB = itk.transformread(transform)[0]
elif phi_AB is None and transform is None:
raise Exception("Specify either transform path or Phi_AB as returned from get_dvf()")
interpolator = itk.LinearInterpolateImageFunction.New(moving)
warped_moving_image = itk.resample_image_filter(
moving,
transform=phi_AB,
interpolator=interpolator,
use_reference_image=True,
reference_image=fixed
)
if save_img is not None:
if warped_moving_out is None:
warped_moving_out="warp.nii.gz"
itk.imwrite(warped_moving_image, warped_moving_out)
else:
return warped_moving_image The output for the script:
Thank you again for your prompt responses and help. I look forward to your reply. |
This is a real puzzler! I see three possibilities for what is going on:
Would you be willing to email me full runnable code and a pair of images from your table? I know that this may be a data sharing issue, but I have reached a dead end with the information I have. I think I need an example I can run and experiment on to resolve this issue. |
Dear Authors,
Thank you for this great content. I was using your model from the source and was trying to register my own data. The results are great but I wanted to obtain the phi_AB, or the net.phi_AB_vectorfield in the original image input dimensions instead of 175x175x175.
I tried to resample the vectorfield using itk.resample_image_filter and the torch.nn.Interpolate fucntion but both resulted in a tensor which was not able to register the images like the field of original network size. I could not figure out how to modify phi_AB since it is a itk.COmpositeTransform format object.
Could you help me understand how to get the net.phi_AB_vectorfield or phi_AB in the dimension of the input image we give.
My issue is similar to #15 but I could not get a solution from that issue.
Would again like to thank you for the help in advance.
The text was updated successfully, but these errors were encountered: