Search code examples
pythonimagenifti

Imaging: Replaces values by 0 over specific threshold


I have data of type float64 which has the size (57, 66, 40). I want to replace all values smaller than 0.9 by 0.

I tried the easiest way without a loop:

img = nib.load('/home/anja/aw.nii')

data = img.get_fdata()

threshold_data = data[data<0.9] == 0

But then I get threshold_data as type bool with size of (55318,). So I lost the dimensions of my image. Can someone help me how to do this, that I still have my image of dimension (57, 66, 40) and just replace values <0.9 in there by 0.


Solution

  • According to nipy docs, the get_fdata() method returns a NumPy array, so the easiest method is just:

    img = nib.load('/home/anja/aw.nii')
    
    data = img.get_fdata()
    
    data[data<0.9] = 0
    
    print(data.shape)