Know the value range for the image data of a NIFTI image Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Know the value range for the image data of a NIFTI image without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m reading a NIFTI file using Python 3.7 and SimpleITK 1.2.4.

I have used this code to show the header information (you can find the header information here):

import SimpleITK as sitk
flair_file = '/content/gdrive/My Drive/Colab Notebooks/.../FLAIR.nii.gz'

reader = sitk.ImageFileReader()
reader.SetFileName(flair_file)

reader.LoadPrivateTagsOn()
reader.ReadImageInformation()
for k in reader.GetMetaDataKeys():
  v = reader.GetMetaData(k)
  print("({0}) = ="{1}"".format(k,v))

print("Image Size: {0}".format(reader.GetSize()))
print("Image PixelType: {0}".format(sitk.GetPixelIDValueAsString(reader.GetPixelID())))

And this is its output:

(ITK_FileNotes) = =""
(aux_file) = =""
(bitpix) = ="32"
(cal_max) = ="0"
(cal_min) = ="0"
(datatype) = ="16"
(descrip) = =""
(dim[0]) = ="3"
(dim[1]) = ="240"
(dim[2]) = ="240"
(dim[3]) = ="48"
(dim[4]) = ="1"
(dim[5]) = ="1"
(dim[6]) = ="1"
(dim[7]) = ="1"
(dim_info) = ="�"
(intent_code) = ="0"
(intent_name) = =""
(intent_p1) = ="0"
(intent_p2) = ="0"
(intent_p3) = ="0"
(pixdim[0]) = ="1"
(pixdim[1]) = ="0.958333"
(pixdim[2]) = ="0.958333"
(pixdim[3]) = ="3"
(pixdim[4]) = ="0"
(pixdim[5]) = ="0"
(pixdim[6]) = ="0"
(pixdim[7]) = ="0"
(qform_code) = ="2"
(qform_code_name) = ="NIFTI_XFORM_ALIGNED_ANAT"
(qoffset_x) = ="118.611"
(qoffset_y) = ="127.182"
(qoffset_z) = ="13.3168"
(quatern_b) = ="0.0315572"
(quatern_c) = ="-0.216054"
(quatern_d) = ="0.975691"
(scl_inter) = ="0"
(scl_slope) = ="1"
(sform_code) = ="1"
(sform_code_name) = ="NIFTI_XFORM_SCANNER_ANAT"
(slice_code) = ="�"
(slice_duration) = ="0"
(slice_end) = ="0"
(slice_start) = ="0"
(srow_x) = ="-0.955749 -0.048177 0.160403 118.611"
(srow_y) = ="0.0220411 -0.868189 -1.26837 127.182"
(srow_z) = ="0.0667887 -0.402902 2.71395 13.3168"
(toffset) = ="0"
(vox_offset) = ="352"
(xyzt_units) = =""
Image Size: (240, 240, 48)
Image PixelType: 32-bit float

Using the imageJ program, this what I get when I click on Image => Show info…

Title: FLAIR.nii.gz
Width:  230.0000 mm (240)
Height:  230.0000 mm (240)
Depth:  144.0000 mm (48)
Size:  11MB
X Resolution:  1.0435 pixels per mm
Y Resolution:  1.0435 pixels per mm
Voxel size: 0.9583x0.9583x3.0000 mm^3
ID: -2
Bits per pixel: 32 (float)
Display range: 0 - 1980.8942
Image: 1/48
No threshold
ScaleToFit: false
Uncalibrated
Path: /FLAIR.nii.gz
Screen location: 927,131 (1920x1080)
Coordinate origin:  0,0,0
No overlay
No selection

And, when I click on Image => Properties, I get that the image is one channel.

If I want to know the value range for the image data, how can I know it? Because the bits per pixel are 32, and the image has one channel, but I don’t know if their values go from 0.0 to 1.0, or from 0.0 to 255.0.

For image data I mean the value for each pixel.

UPDATE

I have used Numpy to know the max a min values from the image data with this code:

import SimpleITK as sitk
flair_file = '/content/gdrive/My Drive/Colab Notebooks/.../FLAIR.nii.gz'

images = sitk.ReadImage(flair_file)

images_array = sitk.GetArrayFromImage(images)
print("Images Array Shape: ", images_array.shape)
print(np.amin(images_array),np.amax(images_array))

And this is its output:

Images Array Shape:  (48, 240, 240)
0.0 2380.6191

Maybe, the min and max value that I’m looking for are: 0.0 and 2380.6191.

Answer

You can use SimpleITK’s StatisticsImageFilter to compute the minimum, maximum, sum, mean, variance sigma of an image. Add the following lines to your code:

img = reader.Execute()
stats = sitk.StatisticsImageFilter()
stats.Execute(img)
print("Minimum:", stats.GetMinimum())
print("Maximum:", stats.GetMaximum())

Here’s the filter’s documentation: https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1StatisticsImageFilter.html

We are here to answer your question about Know the value range for the image data of a NIFTI image - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji