def test_nibabel(self): fn = ants.get_ants_data('mni') ants_img = ants.image_read(fn) nii_mni = nib.load(fn) ants_mni = ants_img.to_nibabel() self.assertTrue((ants_mni.get_qform() == nii_mni.get_qform()).all()) temp = ants.from_nibabel(nii_mni) self.assertTrue(ants.image_physical_space_consistency(ants_img, temp))
def read_nib(addr): # img = np.array(nib.load(addr).dataobj) img = nib.load(addr) ants_data = ants.from_nibabel(img) resampled = ants.resample_image(ants_data, (1.5, 1.5, 1.5)).numpy() # Resample to 1.5mm3 resolution print("Resampled Size: ", resampled.shape) np_resampled_pad = np.pad(resampled, 100, 'constant', constant_values=resampled.min()) # np_resampled_pad = np.pad(img, 100, 'constant', constant_values=img.min()) return crop_center(np_resampled_pad, 256, 256, 400)
def to_ants(image: Union[ArrayOrNifti, ants.ANTsImage]) -> ants.ANTsImage: if isinstance(image, ants.ANTsImage): ants_image = image elif isinstance(image, NiftiImage): ants_image = ants.from_nibabel(image) elif isinstance(image, Array): ants_image = ants.from_numpy(image) else: raise ValueError( "Provided image must be an ANTsImage, Nifti1Image, or np.ndarray." f" Got {type(image)}.") return ants_image
def __init__( self, template: Optional[NiftiImage] = None, type_of_transform: str = "Affine", interpolator: str = "bSpline", metric: str = "mattes", initial_rigid: bool = True, ): if template is None: logger.info("Using MNI (in RAS orientation) as template") standard_mni = ants.get_ants_data("mni") self.template = ants.image_read(standard_mni).reorient_image2( "RAS") else: logger.debug("Loading template") self.template = ants.from_nibabel(template) self.type_of_transform = type_of_transform self.interpolator = interpolator self.metric = metric self.initial_rigid = initial_rigid
def preprocess( image: NiftiImage, mask: Optional[NiftiImage] = None, resolution: Optional[Tuple[float, float, float]] = None, orientation: str = "RAS", n4_convergence_options: Optional[dict] = None, interp_type: str = "linear", second_n4_with_smoothed_mask: bool = True, ) -> Tuple[NiftiImage, NiftiImage]: """Preprocess an MR image Preprocess an MR image according to a simple scheme: 1) N4 bias field correction 2) resample to X mm x Y mm x Z mm 3) reorient images to RAI Args: image: image to preprocess mask: mask covering the brain of image (none if already skull-stripped) resolution: resolution for resampling. None for no resampling. orientation: reorient the image according to this. See ANTsPy for details. n4_convergence_options: n4 processing options. See ANTsPy for details. interp_type: interpolation type for resampling choices: linear, nearest_neighbor, gaussian, windowed_sinc, bspline second_n4_with_smoothed_mask: do a second N4 with a smoothed mask often improves the bias field correction in the image Returns: preprocessed image and corresponding foreground mask """ if n4_convergence_options is None: n4_convergence_options = {"iters": [200, 200, 200, 200], "tol": 1e-7} logger.debug(f"N4 Options are: {n4_convergence_options}") if isinstance(image, nib.Nifti1Image): image = ants.from_nibabel(image) if mask is not None: if isinstance(mask, nib.Nifti1Image): mask = ants.from_nibabel(mask) else: mask = image.get_mask() logger.debug("Starting bias field correction") image = ants.n4_bias_field_correction(image, convergence=n4_convergence_options) if second_n4_with_smoothed_mask: smoothed_mask = ants.smooth_image(mask, 1.0) logger.debug("Starting 2nd bias field correction") image = ants.n4_bias_field_correction( image, convergence=n4_convergence_options, weight_mask=smoothed_mask, ) if resolution is not None: if resolution != mask.spacing: logger.debug(f"Resampling mask to {resolution}") mask = ants.resample_image( mask, resolution, use_voxels=False, interp_type=interp_type_dict["nearest_neighbor"], ) if resolution != image.spacing: logger.debug(f"Resampling image to {resolution}") image = ants.resample_image( image, resolution, use_voxels=False, interp_type=interp_type_dict[interp_type], ) image = image.reorient_image2(orientation) mask = mask.reorient_image2(orientation) image = image.to_nibabel() mask = mask.to_nibabel() return image, mask