Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Images not aligned when applying inverse transform

See original GitHub issue

Issue Description

Describe the bug I am attempting to align two images using a rigid body transformation, then applying the inverse of the registration. The initial registration works as expected but the application of the inverse does not give expected results. See screenshots for details. The first screenshot is the initial alignment and the second is the application of the inverse to the original fixed image.

To Reproduce The call for the registration is:

flair_txfm = ants.registration(fixed=flair_bet, moving=flair_ref, type_of_transform="Rigid")

Then I apply the inverse with:

flair_bet_reg = ants.apply_transforms(moving=flair_bet, fixed=flair_ref, transformlist=flair_txfm['invtransforms'])

The images are: flair_bet: ANTsImage (RPI) Pixel Type : float (float32) Components : 1 Dimensions : (512, 512, 26) Spacing : (0.4297, 0.4297, 5.9996) Origin : (-138.1599, 33.0662, -70.0059) Direction : [ 0.9421 0.3352 -0.0132 0.3343 -0.9347 0.1211 -0.0282 0.1185 0.9926]

flair_ref: ANTsImage (RPI) Pixel Type : float (float32) Components : 1 Dimensions : (156, 156, 36) Spacing : (1.1667, 1.3974, 5.0556) Origin : (-90.0, 126.0, -72.0) Direction : [ 1. 0. 0. 0. -1. 0. 0. 0. 1.]

Expected behavior I would expect the second step (applying inverse) to generate similar results to the initial transformation. In other words, I would expect the images to be aligned.

Screenshots CleanShot 2022-05-31 at 14 51 10@2x

CleanShot 2022-05-31 at 14 52 02@2x


  • OS: MacOS 11.5
  • antspy 0.3.2
  • python 3.9

Please let me know if you need any other info.


Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

ntustisoncommented, Jun 1, 2022

The set of “inverse transforms” is simply the list of transform files stored in the correct order. However, in the case of a linear transform, we only write the forward transform to disk and depend on the user to specify the linear transform which needs to be inverted on the fly. It’s a bit suboptimal but this choice is also based on a legacy relationship with both the ANTsR implementation and the original C++ command line version.

ntustisoncommented, May 31, 2022
Screen Shot 2022-05-31 at 4 06 51 PM

Turn on the verbose output and make sure your output looks something like:

>>> flair_bet_reg = ants.apply_transforms(flair_ref, flair_bet, transformlist=flair_txfm['invtransforms'], whichtoinvert=[True], verbose=True)
['-d', '3', '-i', '0x600002c30d80', '-o', '0x600002c246c0', '-r', '0x600002c24780', '-n', 'linear', '-t', '[/var/folders/ty/33w2ntbj3kn_m_8jxvkzh6t80000gn/T/tmphzmyojtr0GenericAffine.mat,1]']
Using single precision for computations.
Input scalar image: 0x600002c30d80
Reference image: 0x600002c24780
The composite transform comprises the following transforms (in order): 
  1. inverse of /var/folders/ty/33w2ntbj3kn_m_8jxvkzh6t80000gn/T/tmphzmyojtr0GenericAffine.mat (type = AffineTransform)
Default pixel value: 0.0000e+00
Interpolation type: LinearInterpolateImageFunction
Output warped image: 0x600002c246c0
>>> ants.image_write(flair_bet_reg, "~/Desktop/inverseWarped.nii.gz")
>>> flair_ref.plot(overlay=flair_bet_reg, overlay_alpha=0.5)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Image Alignment and Image Warping - - Mael Fabien
We'll now cover the different types of linear transformations that we can apply to an image using inverse-warping. a. Uniform Scaling. Scaling by...
Read more >
Image alignment with ORB and RANSAC in scikit-image
The transform modelled by RANSAC should then be able to align my images. The process appears to work well, I get plenty of...
Read more >
[CV] 14. Image Alignment (Transformation Estimation) | by jun94
One application is to estimate the transformation between images so that images can be stitched (aligned). Figure 2. Image stitching ...
Read more >
Image Alignment and Stitching: A Tutorial1
This tutorial reviews image alignment and image stitching algorithms. ... the second one), and take the inverse transform of the result.
Read more >
Automated alignment of ROI to reference image
Hello, I am attempting to align a mouse coronal DAPI image (ROI of a reference image [floating image]) to a reference image [reference ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found