Can any one help me to how I can scale my pet image.nii file data from -ive - +ive to 0-1?

I am new to medical images. I have to understand the spm12 and perform the pre-processing. Now, I have found that .nii files have negative to positive intensity of images. I don’t know how to scale these values between 0 to 1. Without losing any information.

I would really appreciate it if someone could help me.
Thank you.

Hi @_IT_REHMAN_ABDUL and welcome to neurostars!

Here is an example of doing that in Nilearn:

import nibabel as nib
from nilearn import image
from nilearn import plotting
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

# Load an example image (replace '/path/tp/your/image.nii.gz' with the path to your image)
image_path = '/path/to/your/image.nii.gz'
img = nib.load(image_path)

# Display the original image
plotting.plot_img(img, cut_coords=(0, 0, 0), colorbar=True)
plt.title('Original Image')
plt.show()

# Rescale intensity values to the range [0, 1]
data = img.get_fdata()
data_reshaped = data.reshape(-1, 1)  # Reshape to 1D array
scaler = MinMaxScaler(feature_range=(0, 1))
data_scaled = scaler.fit_transform(data_reshaped).reshape(data.shape)
rescaled_img = nib.Nifti1Image(data_scaled, img.affine)

# Display the rescaled image
plotting.plot_img(rescaled_img, cut_coords=(0, 0, 0), colorbar=True)
plt.title('Rescaled Image (0 to 1)')
plt.show()

# Save the rescaled image to a new file
output_path = 'rescaled_image.nii.gz'
nib.save(rescaled_img, output_path)

Here is how it looks with a T1w image I had.


Best,
Steven

1 Like

Hi Steven, Thank you so much for your quick response and help. I have understand the code. I have some queries. Can you please guide me?
My first query is I am working on brain PET FDG dataset. Is it okay to use minmax function for scaling the dataset? It will ensure no information will be lose.

My Second query is I want to make a dataset from .nii files to .npy for all subjects that I have and give it to the model for training. I just want to train model on axial view. Can please also guide me on this what is the correct way to do this?

Thank you one again.

Regards,
Abdul Rehman

Hi @_IT_REHMAN_ABDUL ,

I don’t work with PET but I can do my best to answer.

No information should be lost as long as you don’t truncate decimals too early.

For this example, the axial slice can be found by getting all values on a given Z-coordinate, so something like axial_slice = data_scaled[:,:,index], which can then be saved out with np.save if you want it in .npy.

Best,
Steven

1 Like

Thank you Steven for your help. It’s helpful for me. Have you experienced the SPM12 Tool? I want to remove the skull using the MNI152 template but fortunately, I cannot find any tutorial on this, some tutorials are available to remove the skull using GM, WM, and CSF templates but I want to remove it using the MNI152 template for PET images.

You might consider using Synthstrip from FreeSurfer: SynthStrip

1 Like

Thank you Steven You help me alot.