Optimum window and levels for vtkResliceImageViewer

I am using vtkNIFTIImageReader to read a nifti file(MRI, human head) and for displaying data I am making use of vtkResliceImageViewer. I see that different files have to different would be

I am using hardcoded values for window and levels as shown below


The problem is that the hardcoding is not suitable for all the datasets. Some datasets appear too bright as shown below:
Is there a computation I should be doing for calculating the proper window and levels?. I also observe that all of my datasets appear with nice intensities in Slicer3D. I guess that there must be some simple calculation to derive an optimum window and level. Any thoughts?

There is no such things as optimal window/level for viewing a medical image, as it depends on the clinical goal (for example, on the same CT, you need different setting if you are interested in the brain tissue or in skull). If you want to have a reasonable default value then you have a few options:

  • DICOM files often contain recommended window center&width values (in 3D Slicer, we use this as default, if such values are present in the image)
  • You can do a simple histogram analysis of the image an make sure that large percentage of voxels are included in the displayed intensity window (in 3D Slicer, we use 0.1-99.9 range, computed by vtkHistrogramStatistics, see full source code here)
  • If your application targets specific imaging modalities and clinical applications then you may use predefined presets (e.g., 3D Slicer offers presets, such as CT-bone, CT-air, CT-abdomen, CT-brain, CT-lung, PET, DTI)

You are free to copy-paste any code from 3D Slicer, but it is easier to use 3D Slicer (or other similar frameworks, such as MITK) as a basis of your application instead. It is less work to begin with (other than some learning about how things work and what features are available) and less work to maintain (you get fixes, upgrades, and new features for free).