'Replace intensity voxel values with probabilities
I would like to know how to replace the intensity values from the voxels of an image (nifti) by probability values (0-1) to generate a plot in colors. I have two files, the original image in nifti and the probabilities in a CSV (rows = voxels, column = probabilities).
Solution 1:[1]
If the size of your .csv file is the same as your NIfTI volumes, then if the number of voxels is n, the number of rows in your .csv file should also be n. And if the NIfTI file is 3D (one volume), your .csv file should have only 1 column (more columns would correspond to different volumes in a 4D time series).
In this simple case you should be able to use np.genfromtxt for the csv file to read the probability and nibabel to read/write the NIfTI file:
import nibabel as nb;
import numpy as np;
probabilities = np.genfromtxt ( 'myColumn.csv' );
inFile = nb.load ( 'MyVolume.nii.gz' );
outFile = nb.Nifti1Image ( probabilities, inFile.affine );
outFile.to_filename ( 'MyProbs.nii.gz' );
You may need to re-shape the probabilities array; in the absence of more specific information I have assumed it all fits.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alle_meije |
