Code Monkey home page Code Monkey logo

Comments (11)

romainVala avatar romainVala commented on June 23, 2024 2

oh I missed the link of the data
thanks @fepegar for the fix

I think this is a typical errors which is not easy to understand when one is not use to the 3D affine
@simonebonato to better understand
try the torchio ploting utility

compare
subject.plot()
and
fixed.plot()

taking into account the affine (and thus the voxel size) is important

from torchio.

simonebonato avatar simonebonato commented on June 23, 2024 2

Thank you both @fepegar and @romainVala for solving my problem.

And thanks @romainVala I did what you told me and seen the difference.

compare subject.plot() and fixed.plot()

Next time I will check directly the spacing first before making any operation.

from torchio.

romainVala avatar romainVala commented on June 23, 2024

note that in the code you apply other augmentation on scaled_subject but not on can_spac_subsject
not sure if it make any difference

from torchio.

simonebonato avatar simonebonato commented on June 23, 2024

Thank you for the suggestion.

I tried applying the transforms RandomFlip and RandomAffine individually, since they affect the geometry of the CT and the label mask, and not the pixel intensities.

I found out that the issue comes with RandomAffine. I tried changing the hyperparameter "center" (image or origin) and "isotropic" (True or False), in all their combinations, but the misalignment still persists.

I then checked the spacing of the CT and the segmentation mask and I got the following:

image

So could it be that the reason could be that they have different spacings? And if I try to give them both the same spacing using Resample(1.0) it's also going to change the resolution of the image and at that point I lose the alignment anyway...

Any idea on how to solve this? Otherwise I will see what spatial transformation cause this issue and which don't and that's it.

from torchio.

romainVala avatar romainVala commented on June 23, 2024

ok difficult to figure out, without an example, (if you can share on volume)

it is weird to have the exact same matrix size but a very different voxel size .. :
this mean that you CT volume has a FOV twice bigger (in the z direction) compare to the segmentation ...

testing the superposition with different voxel size, may be handing, and results may depend of the used viewer

in python I am not sure but it may display the matrix (without taking into acount the voxel size for instance) With mrview (viewer from mrtrix,) or Slicer you will get more correct comparison

If the two are well alligned in Slicer (or mrview) then I would test to use
tresample = tio.Resample(target='CT', include = 'segmentation')
or the opposite depending on the desire final resolution
tresample = tio.Resample(target='segmentation', include = 'CT')

but I suspect that the affine (and thus the voxel size) of the segmentation is not correct and it should be the same as the CT volume ... (was the segmentation not defined from the CT scan ??? if yes, then it should have the exact same voxel size (and affine) )

in this case you should use to tio.CopyAffine('CT') transform to correct the segmentation

from torchio.

fepegar avatar fepegar commented on June 23, 2024

Hi, @simonebonato. As @romainVala suggested, you can use CopyAffine as the firstr transform, as it seems that your segmentations is simply lacking the spatial metadata:

copy_metadata = tio.CopyAffine('CT')

However, I just tried that and the transform did nothing in my example! I'll debug this and come back to you.

from torchio.

fepegar avatar fepegar commented on June 23, 2024

For visibility, this is somehow not working!

import torch
import torchio as tio
ct = tio.ScalarImage('/tmp/upload_folder/case_01_IMG_CT.nrrd')
seg = tio.LabelMap('/tmp/upload_folder/case_01_segmentation.nrrd')
seg.affine = ct.affine
print(seg.affine)
array([[-1.,  0.,  0.,  0.],
       [ 0., -1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

from torchio.

fepegar avatar fepegar commented on June 23, 2024

Update: seg[tio.AFFINE] gives me the expected output. Maybe Image shouldn't inherit from dict.

from torchio.

fepegar avatar fepegar commented on June 23, 2024

Ok, this happens because the affine is not being read correctly as the segmentation hasn't been loaded. That will be addressed.

if self._loaded or self._is_dir() or self._is_multipath():
affine = self[AFFINE]
else:
assert self.path is not None
assert isinstance(self.path, (str, Path))
affine = read_affine(self.path)

Back to this issue.

from torchio.

fepegar avatar fepegar commented on June 23, 2024
import torch
import torchio as tio
ct = tio.ScalarImage('/tmp/upload_folder/case_01_IMG_CT.nrrd')
seg = tio.LabelMap('/tmp/upload_folder/case_01_segmentation.nrrd')

seg.data;  # just to temporarily overcome the bug we mentioned

subject = tio.Subject(ct=ct, seg=seg)

copy_metadata = tio.CopyAffine('ct')
fixed = copy_metadata(subject)
fixed.seg.save('/tmp/upload_folder/case_01_segmentation_fixed.nrrd')

The image seems aligned after copying the affine.

image

All looking good after applying RandomAffine:

transform = tio.RandomAffine()

torch.manual_seed(0)
transformed = transform(fixed)
transformed.ct.save('/tmp/upload_folder/case_01_IMG_CT_transformed.nrrd')
transformed.seg.save('/tmp/upload_folder/case_01_segmentation_fixed_transformed.nrrd')

image

from torchio.

fepegar avatar fepegar commented on June 23, 2024

For visibility, this is somehow not working!

import torch
import torchio as tio
ct = tio.ScalarImage('/tmp/upload_folder/case_01_IMG_CT.nrrd')
seg = tio.LabelMap('/tmp/upload_folder/case_01_segmentation.nrrd')
seg.affine = ct.affine
print(seg.affine)
array([[-1.,  0.,  0.,  0.],
       [ 0., -1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

This has been fixed in v0.18.91.

from torchio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.