SpatialResample vs ResampleToMatch vs Spacing #6248
-
Hi. I'm looking into the resampling transforms to get a common pixel size (e.g. 0.1 um/px) for all my 2D images across training/validation/test splits. I'm confused about the distinction between these 3 transforms:
I understand an integral part of these transforms is the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
one possible option would be making a customized transform to assign the pixel size and SpacingD to normalize them: import os
import numpy as np
import monai
from monai.transforms import Compose, Lambda, LoadImageD, SpacingD
class AssignPixelSize:
def __init__(self, key, pixel_key):
self.key = key
self.pixel_key = pixel_key
def __call__(self, data_dict):
print(data_dict[self.pixel_key])
data_dict[self.key].affine[0, 0] = data_dict[self.pixel_key][0]
data_dict[self.key].affine[1, 1] = data_dict[self.pixel_key][1]
return data_dict
filename = os.path.join(".", "MONAI-logo_color.png")
monai.apps.download_url("https://monai.io/assets/img/MONAI-logo_color.png", filepath=filename)
transform = Compose(
[
LoadImageD(keys="image", image_only=True, ensure_channel_first=True, dtype=np.uint8),
Lambda(func=AssignPixelSize(key="image", pixel_key="pixel_size")),
SpacingD(keys="image", pixdim=(0.1, 0.1)),
]
)
test_data = {"image": filename, "pixel_size": (0.18, 0.17)}
result = transform(test_data)
print(f"image data shape:{result['image'].shape}")
print(result["image"].affine) |
Beta Was this translation helpful? Give feedback.
one possible option would be making a customized transform to assign the pixel size and SpacingD to normalize them: