简体   繁体   中英

How to read multiple 3d images and store them in 4D array using numpy python?

I have used the code below to create an array with the shape of (2, 3, 365, 256, 256) (2, 365, 256, 256) for 3 images however I need my shape to be (2,365, 256, 256, 3) (2, 365, 256, 256) for my model to run, any tips please?

def Load_function(path):
  f_img= nib.load(path )
  img_data= f_img.get_fdata()
return img_data


 def __load__(self, id_name):

    image_path = os.path.join(self.path, id_name)
    ## Reading Image
    image = np.empty((0,365, 256, 256))                    

    for imname in ["image2B.nii.gz", "image1to2_nlB.nii.gz", "diffFSL.nii.gz"]:
        img = Load_function(os.path.join(image_path,imname))

        img = resize_data(img)
        
        ## Normalizaing 
        img = img/np.percentile(img,99.5)
        #images.append(img)
        #images.append(img)
    ## store into a 4D array that you’ve created above (this will hold all the images you want for one subject)
        image = np.append(img[np.newaxis, ...],image, axis=0)  # add a new axis to each image and append them to result
          
    ## Reading Masks
    mask = Load_function(os.path.join(image_path, "ground_truth.nii.gz"))
    mask = resize_data(mask)
    
    return image, mask

您可以尝试在 NumPy 中进行转置,检查此https://numpy.org/doc/stable/reference/generated/numpy.transpose.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM