Comments (11)
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.
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.
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.
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:
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.
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.
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.
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.
Update: seg[tio.AFFINE]
gives me the expected output. Maybe Image
shouldn't inherit from dict
.
from torchio.
Ok, this happens because the affine is not being read correctly as the segmentation hasn't been loaded. That will be addressed.
torchio/src/torchio/data/image.py
Lines 255 to 260 in 0933def
Back to this issue.
from torchio.
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.
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')
from torchio.
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)
- The Affine matrix does not change after applying the augmentations HOT 3
- Custom loader not used when loading data lazily HOT 2
- Seed is not working HOT 2
- Silenced exception makes it harder to debug custom Transforms HOT 5
- Resample
- tio.Resample does not work with custom image class HOT 2
- Setting NUM_SAMPLES when using sampler with Queue HOT 3
- RescaleIntensity - multiple calls HOT 9
- Return sampled parameters upon request HOT 3
- Halve queue length when using DDP HOT 2
- bug in rotation part of tio.transforms.RandomAffine HOT 4
- get_subjects_from_batch has a hick-up with int metadata HOT 5
- masking_method in Mask class is not saved as argument (preventing applying the inverse transform)
- RandomAffine raises an error when isotropic=True and 3 elements are given for scales HOT 10
- Queue is not respecting the batch size HOT 1
- Resample an image by providing only the target affine HOT 1
- Supporting PyTorch 2.3 HOT 23
- Cannot copy subclass of Subject with keyword arguments HOT 2
- Image intensity augmentations HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from torchio.