@@ -1274,10 +1274,11 @@ def n4_bias_field_correction(
12741274 import ants .ops .bias_correction as bc # install antspyx not ants!
12751275 except ModuleNotFoundError :
12761276 import ants .utils .bias_correction as bc # install antspyx not ants!
1277- from ants .utils .convert_nibabel import from_nibabel
12781277 from scipy .ndimage import binary_dilation , generate_binary_structure
1278+
1279+ from TPTBox .core .internal import ants_to_nifti , nifti_to_ants
12791280 dtype = self .dtype
1280- input_ants :ants .ANTsImage = from_nibabel ( nib . nifti1 . Nifti1Image ( self .get_array (), self . affine ) )
1281+ input_ants :ants .ANTsImage = self .to_ants ( )
12811282 if threshold != 0 :
12821283 mask_arr = self .get_array ()
12831284 mask_arr [mask_arr < threshold ] = 0
@@ -1286,7 +1287,7 @@ def n4_bias_field_correction(
12861287 struct = generate_binary_structure (3 , 3 )
12871288 mask_arr = binary_dilation (mask_arr .copy (), structure = struct , iterations = 3 )
12881289 mask_arr = mask_arr .astype (np .uint8 )
1289- mask :ants .ANTsImage = from_nibabel ( nib . nifti1 . Nifti1Image ( mask_arr , self .affine )) #self. set_array(mask ,verbose=False).nii
1290+ mask :ants .ANTsImage = self .set_array (mask_arr ,verbose = False ).to_ants ()
12901291 mask = mask .set_spacing (input_ants .spacing ) # type: ignore
12911292 out = bc .n4_bias_field_correction (
12921293 input_ants ,
@@ -1299,11 +1300,10 @@ def n4_bias_field_correction(
12991300
13001301 )
13011302
1302-
1303- out_nib :Nifti1Image = out .to_nibabel ()
1303+ out_nib :Nifti1Image = ants_to_nifti (out )
13041304 if crop :
13051305 # Crop to regions that had a normalization applied. Removes a lot of dead space
1306- dif = NII ((input_ants - out ). to_nibabel ( ))
1306+ dif = NII (ants_to_nifti (input_ants - out ))
13071307 dif_arr = dif .get_array ()
13081308 dif_arr [dif_arr != 0 ] = 1
13091309 dif .set_array_ (dif_arr ,verbose = verbose )
@@ -1513,7 +1513,14 @@ def to_ants(self) -> Any:
15131513 log .print_error ()
15141514 log .on_fail ("run 'pip install antspyx' to install hf-deepali" )
15151515 raise
1516- return ants .from_nibabel (self .nii )
1516+ try :
1517+ from ants .utils .convert_nibabel import from_nibabel
1518+
1519+ return from_nibabel (self .nii )
1520+ except ModuleNotFoundError :
1521+ from ants .utils .nibabel_nifti_to_ants import from_nibabel_nifti
1522+
1523+ return from_nibabel_nifti (self .nii )
15171524
15181525 def to_simpleITK (self ) -> Any :
15191526 """Converts this NII to a SimpleITK image.
0 commit comments