def pre_process(self, dictionnaire: dict) -> BytesIO:
        """[Pre_process for pytorch]

        Args:
            dictionnaire (dict): [Dictionary containing the id of the images]

        Returns:
            BytesIO: [description]
        """
        dict = dictionnaire
        data_path = settings.STORAGE_DIR
        idImage = str(dict['id'])
        path_image = data_path + '/image/image_' + idImage + '_CT.nii'
        objet = Nifti(path_image)
        resampled = objet.resample(shape=(256, 256, 1024))
        mip_generator = MIP_Generator(resampled)
        array = mip_generator.project(angle=0)
        array[np.where(array < 500)] = 0  #500 UH
        array[np.where(array > 1024)] = 1024  #1024 UH
        array = array[:, :, ] / 1024
        array = np.expand_dims(array, axis=0)
        array = array.astype(np.double)
        np_bytes = BytesIO()
        np.save(np_bytes, array, allow_pickle=True)
        np_bytes = np_bytes.getvalue()
        return np_bytes
def preprocessing_image_to_bytes(image: str,
                                 output_shape: tuple,
                                 angle: int = 0) -> torch.Tensor:
    """
    Preprocessing applied to an image to fit the input data for classification neural network
    Resample to the output shape 
    Scale Range intensity 
    Transform to torch array

    Args:
        image (str): path to the nifti file 
        output_shape (tuple): shape of the image required by the neural network
        angle (int): angle to be applied to the CT image

    Returns: 
        (torch.Tensor): Tensor of the preprocessed image (input for neural classification neural network)
    
    """
    objet = Nifti(image)
    resampled = objet.resample(output_shape)
    mip_generator = MIP_Generator(resampled)
    array = mip_generator.project(angle=angle)
    array[np.where(array < 500)] = 0  #500 UH
    array[np.where(array > 1024)] = 1024  #1024 UH
    array = array[:, :, ] / 1024
    array = np.expand_dims(array, axis=0)
    array = array.astype(np.double)
    array = np.expand_dims(array, axis=0)
    array = torch.from_numpy(np.array(array))
    return array
Ejemplo n.º 3
0
 def generate_mip(self,idImage:str):
     data_path = settings.STORAGE_DIR
     directory=settings.STORAGE_DIR+'/image'
     path_ct =data_path+'/image/image_'+idImage+'.nii'
     objet = Nifti(path_ct)
     resampled = objet.resample(shape=(256, 256, 1024))
     resampled[np.where(resampled < 500)] = 0 #500 UH
     normalize = resampled[:,:,:,]/np.max(resampled)
     mip_generator = MIP_Generator(normalize)
     mip_generator.project(angle=0)
     print(mip_generator.project(angle=0))
     mip_generator.save_as_png('image_2D_'+idImage,  directory, vmin=0, vmax=1)       
Ejemplo n.º 4
0
def preprocessing(path_image, output_shape, angle):
    objet = Nifti(path_image)
    resampled = objet.resample(shape=output_shape)
    mip_generator = MIP_Generator(resampled)
    array = mip_generator.project(angle=angle)
    array[np.where(array < 500)] = 0  #500 UH
    array[np.where(array > 1024)] = 1024  #1024 UH
    array = array[:, :, ] / 1024
    array = np.expand_dims(array, axis=0)
    array = array.astype(np.double)
    np_bytes = BytesIO()
    np.save(np_bytes, array, allow_pickle=True)
    np_bytes = np_bytes.getvalue()
    return np_bytes
Ejemplo n.º 5
0
    def __call__(self, sample:dict)->dict:
        """
        Reshape and scale intensity range of the image. 
        Transforms the data into torch tensors for the neural network.

        Args: 
            sample (dict): dictionnary of the input data with the original image

        Returns: 
            (dict): dictionnary of the input data with the transformed image. Ready for the neural network.
        """
        objet = Nifti(sample['image'])
        resampled = objet.resample(self.output_shape)
        mip_generator = MIP_Generator(resampled)
        array=mip_generator.project(angle=self.angle)
        array[np.where(array < 500)] = 0 #500 UH
        array[np.where(array > 1024)] = 1024 #1024 UH
        array = array[:,:,]/1024
        array = np.expand_dims(array, axis=0)
        sample  = {'image': torch.from_numpy(np.array(array)), 'head': torch.from_numpy(np.array(sample['head'])),'leg': torch.from_numpy(np.array(sample['leg'])),'right_arm': torch.from_numpy(np.array(sample['right_arm'])),'left_arm': torch.from_numpy(np.array(sample['left_arm'])) }
        return sample
Ejemplo n.º 6
0
def generate_instance_from_nifti(img_dict: dict) -> tuple:
    """prepare a batch for classification model from a dict

    Args:
        img_dict ([dict]): [{ct_img : value(a nifti image.nii), upper_limit : value, lower_limit : value, right_arm : value, left_arm : value}]

    Returns:
        [tuple]: [return the 2D MIP of the CT image and its encoded label]
    """
    resampled_array = Nifti(img_dict['ct_img']).resample(
        shape_matrix=(256, 256, 1024), shape_physic=(700, 700, 2000))
    resampled_array[np.where(resampled_array < 500)] = 0  #500 UH
    normalize = resampled_array[:, :, :, ] / np.max(resampled_array)
    mip_generator = MIP_Generator(normalize)
    mip = mip_generator.project(angle=0)
    mip = np.expand_dims(mip, -1)
    label = encoding_instance(img_dict)

    return mip, label
    def pre_process(self, dictionnaire: dict) -> TensorProto:
        """[Pre_process for TF]

        Args:
            dictionnaire (dict): [Dictionary containing the id of the images]

        Returns:
            TensorProto: [description]
        """
        dict = dictionnaire
        data_path = settings.STORAGE_DIR
        idImage = str(dict['id'])
        nifti_path = data_path + '/image/image_' + idImage + '.nii'
        resampled_array = Nifti(nifti_path).resample((256, 256, 1024))
        resampled_array[np.where(resampled_array < 500)] = 0  #500 UH
        normalize = resampled_array[:, :, :, ] / np.max(
            resampled_array)  #normalize
        mip_generator = MIP_Generator(normalize)
        mip = mip_generator.project(angle=0)
        mip = np.expand_dims(mip, -1)
        mip = np.array(mip).astype('float32')
        return tf.make_tensor_proto(mip, shape=[1, 1024, 256, 1])