Files reading issues. Can you please help?

C:/Users/ Desktop/train/’

PX1_0000- T2W.nii.gz + seg.nii.gz

PX1_0001- T2W.nii.gz+ seg.nii.gz
.
.
… PX1_0200- T2W.nii.gz+ seg.nii

path1='C:/Users/Desktop/train/'

def load_data(path):
  my_dir = sorted(os.listdir(path))
  for p in tqdm(my_dir):
    data1=[]
    gt=[]
    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.append([T2W])
    gt.append(seg)
  data1 = np.asarray(data1,dtype=np.uint8)
  gt = np.asarray(gt,dtype=np.uint8)
  return data1,gt

data1,gt1 = load_data(path1)

Hello @blizzardboi_2020,

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.

Hello @blizzardboi_2020,

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())
    return img

If this does not solve your issue, please provide additional details so that we better understand it.

1 Like

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.

Dear @blizzardboi_2020,

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.

2 Likes

Hi! I solved my problem of reading. In this script the desired size will be an array like this right?

Path=D:/Train/....
img1=read_resample(Path, [384,384,30])

Yes.

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.
Vol=vol1+vol2.

Hello @blizzardboi_2020,

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.

1 Like

Thanks!!! I got it