ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
625 stars 161 forks source link

Failed to allocate memory for image Error #453

Closed novafae closed 1 year ago

novafae commented 1 year ago

I'm trying to run ants.n4_bias_field_correction. Using ANTsPy across multiple machines, I'm getting a "Failed to allocate memory for image." error. However, it works using the N4BiasFieldCorrection command with the same images via ANTs. I

I've included the scripts I use, and the input. I tried searching the GitHub, but couldn't find a solution.

Does anyone have any idea what's my issue is?

import ants
import os
import fae

## USER INPUT ################################################################

# Filenames
img_fName = fae.pc2wsl(r"C:\FK\temp\T_P14_MRI-T2_SymmetricStereotaxicTemplate_50um_Oriented.nii.gz")

# Options 
shrink_factor = 10
verbose = True
ants_type = 0 # 0 for ANTsPy 1 for ANTs

##############################################################################

# Set Filenames
output_fName = os.path.dirname(img_fName) + os.sep + 'n' + os.path.basename(img_fName)
mask_fName = os.path.dirname(img_fName) + os.sep + os.path.basename(img_fName).replace(".nii", '_MASK.nii')

# Read Images
img = ants.image_read ( img_fName )
img[img < 0] = 0
print('img Image: ' )
print(img)
ants.plot(img)

# Make Mask
img_mask = img.clone()
img_mask[img > 0] = 1
ants.image_write(img_mask, mask_fName)

if ants_type:
    # ANTs N4
    cmd = "N4BiasFieldCorrection -d 3 -i " + img_fName + " -r 1 -s " + str(shrink_factor) + " -x " + mask_fName + " -v 1 -o " + output_fName
    os.system(cmd)
elif ants_type == 0:
    # Antspy N4
    img_n4 = ants.n4_bias_field_correction(img, verbose=verbose, rescale_intensities=True, shrink_factor=shrink_factor, mask=img_mask)
    print('img Image: ' )
    print(img_n4)
    ants.plot(img_n4)

    # Save Output
    ants.image_write(ants_n4, output_fName)

Input File: T_P14_MRI-T2_SymmetricStereotaxicTemplate_50um_Oriented.nii.gz

cookpa commented 1 year ago

The default spline spacing is for human brains, much too large for an animal image. This leads to the image being enlarged to fit the spline, using up all the memory.

To fix, add a reasonable spline spacing for your image, something like spline_param = 20, to the N4 call.

cookpa commented 1 year ago

@ntustison what do you think about making the default spline spacing the the largest side of the image bounding box? So in this case (dims = 256x224x320) it would default to 320 * 0.05

ntustison commented 1 year ago

Yeah, I didn't set the default here in ANTsPy. Very much along your suggestion, I would just default to what we do in ANTs which is a single mesh element.

cookpa commented 1 year ago

Oh yes, that would be better. Thanks

novafae commented 1 year ago

Thank you! adding spline_param = 20 worked.

The default spline spacing is for human brains, much too large for an animal image. This leads to the image being enlarged to fit the spline, using up all the memory.

To fix, add a reasonable spline spacing for your image, something like spline_param = 20, to the N4 call.