I'm new with NiBabel. I want to know how to get intensity matrix from a Nifti images with this library. I use the following script to get voxels:
import nibabel as ni
example_img = ni.load('myImage.nii')
data = example_img.get_data()
I thought at beginning that data contains the voxels' intensity, but when I've printed it, I've seen negative values, it seems strange to have a negative intensity within an image, don't you think?
I need to get the voxels' intensity within a nifti image, is it possible with nibabel? If not, can you propose me an other solution?
Not sure how you're getting negative voxel values, but here's a way to display a NifTi image as a matrix:
import nibabel as ni
img = ni.load('myImage.nii')
data = example_img.get_data()
mat = []
for i in range(img.shape[0]):
plane = []
for j in range(img.shape[1]):
row = []
for k in range(img.shape[2]):
row.append(data[i][j][k])
plane.append(row)
mat.append(plane)
Now you can print out/store in a text file the variable "mat".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With