nibabel icon indicating copy to clipboard operation
nibabel copied to clipboard

How to overwrite voxel intensity values?

Open CristianoPizzamiglio opened this issue 2 years ago • 0 comments

Thanks for this amazing library!

Given a nifti file:

import nibabel as ni

example = ni.load("/nifti/path/example.nii.gz")

I can get the voxel intensity values in the following way:

intensities = example.get_fdata()

intensities is a 2D numpy array having shape (600, 512, 512). For this particular case, this array consists of two values only: -1024 and 14336. What I would like to do is to overwrite this intensities array, i.e. to normalize it in the range [0, 1], so the -1024 values must be converted to 0 and the 14336 values must be converted to 1. Eventually, I would like to write the modified nifti file:

# example.set_intensity(my_custom_intensities)
ni.save(example, "/nifti/path/example_modified.nii.gz")

I don't see any set method available for doing that. Maybe should I use example.dataobj.inter and example.dataobj.slope?

Can you help me to understand what I should do, please? Thanks!

CristianoPizzamiglio avatar Jun 22 '22 09:06 CristianoPizzamiglio