简体   繁体   中英

Memmap AttributeError when trying to access nifti image header

After loading a NIFTI (.nii) image (using Nibabel) with the code scan = nibabel.load(filepath) , it is useful to display the image header information via scan.header .

If you call scan.get_fdata() before calling scan.header , the error arises: AttributeError: 'memmap' object has no attribute 'header' . Example of such code:

scan = nibabel.load(test_image.nii)
scan_volume_data = scan.get_fdata()
print(scan.header)

You have to call scan.header before calling scan.get_fdata() . This is because after calling scan.get_fdata() , the image object gets transformed to a memmap (memory-map) object, which loses the header information. Example of correct code is the following:

scan = nibabel.load(test_image.nii)
print(scan.header)
scan_volume_data = scan.get_fdata()

We can observe the change of the image datatype with the following code:

scan = nibabel.load(test_image.nii)
print(type(scan)) # <class 'numpy.memmap'>
scan_volume_data = scan.get_fdata()
print(type(scan)) # <class 'nibabel.nifti1.Nifti1Image'>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM