Description
**
The below code reads a .nii.gz file that has a mask after reading it randomly rotates in x, y, and z axis randomly slices the mask 5 times to get 5 rings, but on the output I'm getting on x-axis the individual rings are not parallel in orientation but in y and z axis the rings are parallel in orientation. Please refer to the video taken from 3D Slicer on the "Y" right bottom you can see the lines are not parallel but the "G" left bottom and "R" left top you can see the lines are parallel and at 90 degree. video
Python version 3.8
MONAI version 1.1.0
Numpy 1.24.2
Pytorch 2.0.0
import numpy as np
import torch
import math
from monai.transforms import LoadImaged, SaveImaged, RandRotated, RandRotate, Compose
from monai.utils import set_determinism
import nibabel as nib
#Set deterministic behavior
set_determinism(seed=7)
num_slices = 5
#1. Load a 3D medical image (.nii file)
keys = ["image"]
filename = "/path/to/input/trans_case_00000.nii.gz"
data = {"image": filename}
#Create a pipeline to load and rotate the image
load_transform = LoadImaged(keys)
affine = load_transform(data)['image_meta_dict']['original_affine']
original_mask = load_transform(data)
rotate_x_transform = RandRotated(keys,range_x = 6, prob=1.0, padding_mode = 'zeros', keep_size= False)
rotate_y_transform = RandRotated(keys,range_y = 6, prob=1.0, padding_mode = 'zeros', keep_size= False)
rotate_z_transform = RandRotated(keys,range_z = 6, prob=1.0, padding_mode = 'zeros', keep_size= False)
r_transforms = Compose([rotate_x_transform, rotate_y_transform, rotate_z_transform])
transforms = Compose([load_transform,r_transforms])
rotated_data = transforms(data)
image = rotated_data["image"]
dict_axis = {'x': 0, 'y': 1, 'z': 2}
noisy_mask = np.zeros_like(original_mask["image"])
print(noisy_mask.shape)
for i in range(num_slices):
print(i)
rotated_data = transforms(data)
image = rotated_data["image"]
random_item = random.choice(list(dict_axis.items()))
random_value = random_item[1]
image_one_slice = image.clone()
slice_indx = np.random.randint(image.shape[random_value])
print("Slice",random_item,random_value,slice_indx)
if (random_value == 0):
# x-axis
image_one_slice[:slice_indx, :, :] = 0
image_one_slice[slice_indx+1:, :, :] = 0
elif(random_value == 1):
# y-axs
image_one_slice[:, :slice_indx, :] = 0
image_one_slice[:, slice_indx+1:, :] = 0
else:
# z-axis
image_one_slice[:, :, :slice_indx] = 0
image_one_slice[:, :, slice_indx+1:] = 0
del random_item, random_value
#4. Apply the inverse transform to show the circular point in the original image
rotated_data["image"] = image_one_slice
original_data = r_transforms.inverse(rotated_data)
noisy_mask = noisy_mask + original_data["image"]
nib.save(nib.Nifti1Image(noisy_mask, affine), "/path/to/output_5rings.nii.gz")