SCIInstitute / ShapeWorks

ShapeWorks
http://sciinstitute.github.io/ShapeWorks/
Other
101 stars 32 forks source link

Applying transform T then T^-1 isn't an identity mapping #2144

Closed topinfrassi01 closed 9 months ago

topinfrassi01 commented 9 months ago

I'm using Image.createCenterOfMassTransformon a segmentation I loaded using shapeworks.Image(path).

I think there might be a problem in the code of applyTransform, however I'm not certain.


image = sw.Image('path_to_segmentation.nii.gz')

image.centerOfMass()
# [ -12.03379557  163.640748   -163.86038154]

transform = image.createCenterOfMassTransform()

#[[ 1.          0.          0.          0.        ]
# [ 0.          1.          0.          0.        ]
# [ 0.          0.          1.          0.        ]
# [-1.18378947 39.74075411 79.78961271  1.        ]]

image.applyTransform(transform)

image.centerOfMass()
# [ -11.02913016  124.55486303 -243.61533568]

image.applyTransform(np.linalg.inv(transform))

image.centerOfMass()
# [ -12.07290568  165.32558063 -164.05359853]

While we can see the differences are not so big, I don't understand why applying these two transforms one after another doesn't give back the same center of mass as before. Is there a problem with my understanding of the transforms API or is this a bug?

akenmorris commented 9 months ago

Applying transforms to an images/segmentations involves resampling the image into to the new space. The typical workflow for transforming based on the center of mass involves trying to center the desired object within the resulting image. The center of mass transform typically won't be translating by multiples of the unit spacing, so the resampling is probably going to produce some artifact at the edges (e.g. partial volume effect). Resampling it again back to the original space is going to give a very similar but likely slightly different image on the edges.

I suppose if you altered the transform to have x, y, and z values being an exact multiple of the image spacing, then you could probably do this exercise and get back the same result. This would not be possible with rotations from rigid transforms, the more common case.

Does that make sense?

topinfrassi01 commented 9 months ago

Hi @akenmorris, thanks for your explanation I do understand. I think this issue can be closed as it answers my question :)