Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get images intensity matrix from Nifti images with Nibabel?

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?

like image 954
chaw359 Avatar asked Sep 01 '25 01:09

chaw359


1 Answers

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".

like image 188
Toby Avatar answered Sep 02 '25 16:09

Toby