Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to allocate memory for image Error #453

Closed
novafae opened this issue Apr 21, 2023 · 5 comments · Fixed by #457
Closed

Failed to allocate memory for image Error #453

novafae opened this issue Apr 21, 2023 · 5 comments · Fixed by #457

Comments

@novafae
Copy link

novafae commented Apr 21, 2023

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
Copy link
Member

cookpa commented Apr 25, 2023

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
Copy link
Member

cookpa commented Apr 25, 2023

@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
Copy link
Member

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
Copy link
Member

cookpa commented Apr 25, 2023

Oh yes, that would be better. Thanks

@novafae
Copy link
Author

novafae commented Apr 25, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants