Resample one key to the spacing of another key #3245
Answered
by
rijobro
mattwarkentin
asked this question in
Q&A
-
Hi, I am trying to write a custom transform that will resample the spacing of the one image/key to match the spacing of another key within a subject. This is my attempt so far, but it errors and I can't figure out why... from monai.transforms import (
Compose,
LoadImaged,
AddChanneld,
ToTensord,
Orientationd,
Spacing,
MapTransform
)
class DynamicSpacingd(MapTransform):
def __init__(self, keys, template_key):
super().__init__(keys)
self.template_key = template_key
def __call__(self, data):
new_spacing = data[f'{self.template_key}_meta_dict']['spacing']
spacer = Spacing(new_spacing.tolist())
for key in self.keys:
data[key] = spacer(data[key])
return data
keys = ['img', 'mask']
trans = Compose([
LoadImaged(keys),
AddChanneld(keys),
Orientationd(keys, axcodes='RAS'),
DynamicSpacingd('mask', 'img'),
ToTensord(keys)
])
sub = {'img': 'image.nrrd', 'mask': 'mask.nrrd'}
trans(sub)
Any idea what I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
rijobro
Nov 2, 2021
Replies: 1 comment 1 reply
-
def __call__(self, data):
new_spacing = data[f'{self.template_key}_meta_dict']['spacing']
return Spacingd(self.keys, new_spacing.tolist())(data) i.e., pass the spacing on to |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mattwarkentin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Spacing
returns a tuple of outputs:return output_data, affine, new_affine
. You could dodata[key] = spacer(data[key])[0]
, or more simply, you could probably do:i.e., pass the spacing on to
Spacingd
rather thanSpacing
.