Trouble using ANTsPy to register Label Image

Summary of what happened:

Hello NeuroStars Community,

I am looking for support related to registration of mouse MRI images. I have tried to use ANTsPy to register images. I set my experimental image as fixed and a Waxholm MRI template as moving and was successfully able to warp the template into the fixed image space. However, I have not succeeded in applying that same set of transformations to the Labels image in a good way. The code hangs if I try to include the call to apply_transforms using the interpolator ‘genericLabel’. The code does work if I use ‘nearestNeighbors’ instead. Do I need to explicity set the pixeltype for the label image differently if I use genericLabel?

I suspect that I am missing something obvious about how one should pull annotations from the template space onto each scanned mouse mouse brain. I am also open to using other tools or software to perform this section of what I hope will be a longer pipeline.

Relatively new to both the neuro and the coding sides of things, so I hope that my post is clear. Looking forward to any advice.

Warmly,
ross

Command used (and if a helper script was used, a link to the helper script or the command generated):

import ants

fixed_image_path = 'E:/PROJECTS/cleared_first_echo_Mag_nR042836w2.nii'
moving_image_path = 'E:/PROJECTS/resampled_template_voxel.nii'

# Read the fixed image and mask using ANTsPy
fixed_image = ants.image_read(fixed_image_path)
moving_image = ants.image_read(moving_image_path)

# Perform registration
registration = ants.registration(fixed=fixed_image, moving=moving_image,
                                 type_of_transform='SyN', verbose = True)

# Save the registered (warped) image
warped_moving_image = registration['warpedmovout']
warped_moving_image_path = 'E:/PROJECTS/warped_moving_image_today_clearedfile.nii'
ants.image_write(warped_moving_image, warped_moving_image_path)

print(f"Registration completed. Warped image saved to {warped_moving_image_path}")

#Above code snippet works - below code doesn't

label_image_path = 'E:/PROJECTS/resampled_WHS_0.5_Labels.nii'
label_image = ants.image_read(label_image_path)

# Apply the transforms to the label image
print("Applying transforms to the label image...")
warped_label_image = ants.apply_transforms(fixed=fixed_image, moving=label_image,
                                           transformlist=registration['fwdtransforms'], interpolator='genericLabel')
label_out = 'E:/PROJECTS/warped_labels.nii'
ants.image_write(warped_label_image, label_out)
print(f"Label transformation completed. Warped image saved to {label_out}")

Environment:

Trying to run this in a Jupyter Notebook

Hi @Ross_Smith, and welcome to neurostars!

I am not familiar with this atlas, and am not sure why the generic label option is causing stalls, but if the registration looks good with the nearest neighbor interpolation (which is commonly used for masks and labels) then I would say that should be usable.

Best,
Steven

In my attempt to illustrate how the genericLabel wasn’t working great, I think I found clarity. The problem stems from when I resampled the label atlas to get the voxel spacing to match my data. In that interpolation step the values did not remain as purely integers. Which I noticed when I opened up the atlas in ITK-snap.

Okay, a bit embarrassing, but I think I can solve this with simply being more careful in the initial resampling.

Thanks for your very speedy response. It did help!

Ah yes, that would definitely do it! I don’t think you need to resample before running a registration. I believe ANTs performs resampling in the registration process based on the geometry of the fixed image.