How to make Custom transformation on monai in a simple way #4809
-
Hi, this is my funtion :
and I want to call it like this in the compose : Thanks in advanced. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
good question, one way to do it is to use the MONAI/monai/transforms/adaptors.py Line 13 in 30d592c import numpy as np
from scipy import ndimage
from monai.transforms import Compose, LoadImageD, adaptor
def PixelSpacing(pxlsp, target_space=[0.4, 0.4, 0.4]):
def pixelspacing(img, pxlsp=pxlsp, target_space=target_space):
orig_shape = img.shape
target_shape = [
int(np.round(orig_shape[0] * (target_space[0] / pxlsp[0]))),
int(np.round(orig_shape[1] * (target_space[1] / pxlsp[1]))),
int(np.round(orig_shape[2] * (target_space[2] / pxlsp[2]))),
]
resize_factor = np.array(target_shape) / np.array(orig_shape)
img = ndimage.zoom(img, resize_factor, order=1)
return img
return pixelspacing
xform = Compose(
[
LoadImageD("my_img_key", image_only=True),
adaptor(PixelSpacing([1.0, 1.0, 1.0]), "output_img_key", {"my_img_key": "img"}),
]
)
output = xform({"my_img_key": "./avg152T1_RL_nifti.nii.gz"})
print(output.keys())
print(output["output_img_key"].shape) |
Beta Was this translation helpful? Give feedback.
good question, one way to do it is to use the
adaptor
API inmonai.transforms
MONAI/monai/transforms/adaptors.py
Line 13 in 30d592c