PX1_0000- T2W.nii.gz + seg.nii.gz
PX1_0001- T2W.nii.gz+ seg.nii.gz
… PX1_0200- T2W.nii.gz+ seg.nii
my_dir = sorted(os.listdir(path))
for p in tqdm(my_dir):
data_list = os.path.join(path+p)
img = sitk.ReadImage(path + p + '/'+'seg.nii.gz')
seg = sitk.GetArrayFromImage(img)
img = sitk.ReadImage(path + p + '/'+ 'T2W.nii.gz')
T2W = sitk.GetArrayFromImage(img)
data1 = np.asarray(data1,dtype=np.uint8)
gt = np.asarray(gt,dtype=np.uint8)
data1,gt1 = load_data(path1)
What exactly is the issue with the file reading? You just provided code without stating the problem.
Hi. The output is showing the size of the contents of 1st folder oy i.e. 19,384x384. I want to get all the shapes from all folder in a boy array. However, some files has different shape(21,384,384) and (23,384,384). Can you tell me where and how should I use resize operation for this? I want the final storage of the array to be (5,19,384,384). I read the docs but stilll finding it hard to do it. if you can make the changes, I will be grateful to you.
My understanding of your request:
You have multiple images with different sizes, and you want to force all of them to have the same size (number of pixels per dimension).
The following code will read an image and resample it to a desired size if there is a need to resample:
import SimpleITK as sitk
def read_resample(file_name, desired_size):
img = sitk.ReadImage(file_name)
actual_size = img.GetSize()
if not all([a_sz == d_sz for a_sz, d_sz in zip(actual_size, desired_size)]):
new_spacing = [(sz-1)*spc/(nsz-1) for nsz,sz,spc in zip(desired_size, actual_size, img.GetSpacing())]
img = sitk.Resample(img, desired_size, sitk.Transform(), sitk.sitkLinear,
img.GetOrigin(), new_spacing, img.GetDirection())
If this does not solve your issue, please provide additional details so that we better understand it.
But before resizing, I am unable to read all the files from 5 folders. Zivy can you please manipulate it according to the folder style(.nii.gz)? That would be great. I am facing difficulties in reading through the files.
This forum is for questions with respect to ITK/SimpleITK and not a general “how to” in Python. I suggest that pursue generic Python questions in more appropriate forums.
More specifically, how to traverse a directory structure,
os.walk() and how to create file names,
os.path.join(). Once you are able to generate the file names specific to your directory structure you should be able to read and resample them with the function provided in the previous post.
Hi! I solved my problem of reading. In this script the desired size will be an array like this right?
Hi Zivy, can we normally add 2 .nii volumes of a single patient (same size) after importing each other separately? Actually some slices cover some information in Vol1, some for Vol 2.
To add two images in SimpleITK their size, spacing, origin, and direction cosine matrices need to match. Please see this Jupyter notebook section titled “Image operations”.
As you are just starting with SimpleITK I highly recommend that you invest a couple of hours and take our new online tutorial which includes both Jupyter notebooks and videos explaining them. This will give you a solid foundation in SimpleITK.