Skip to content
saundersresearch edited this page Jan 26, 2025 · 2 revisions

OpenDIVE Wiki

Overview of the nifti2png command

nifti2png is a command-line utility designed to simplify the visualization of medical imaging data stored in NIfTI (.nii or .nii.gz) format. With nifti2png, users can extract slices from 3D or 4D medical images and render them interactively or save them as PNG files for further use.

This tool is part of a larger project or library and offers flexible options for visualization, including slice orientation, output resolution, and intensity value range adjustments.


Setup and Installation

To use nifti2png, ensure that you have the required dependencies installed. Follow these steps to set up the environment:

  1. Install Python

Ensure that Python is installed on your system. We recommend using a virtual environment such as uv or conda. Then, install the package using:

pip install git+https://github.com/MASILab/open_dive

Usage Instructions

The nifti2png utility provides an easy-to-use command-line interface. Below are the available options and examples.

Command-Line Options

python nifti2png.py [NIfTI file path] [OPTIONS]

Command-line Arguments for plot_nifti

Argument Type Default Description
nifti_path Path Required Path to the NIFTI file to plot.
-s, --slice int or str "m" Slice index (integer) or "m" for the middle slice.
-o, --orientation str "axial" Orientation of the slice. Can be "axial", "sagittal", or "coronal".
--size tuple (600, 400) Size of the window in pixels (width, height).
--save_path str None Optional path to save the plot.
--interactive flag False Whether to interactively show the scene.
--value_range int, int None Optional value range for the slicer. Default is the min/max of the image.
--volume_idx int None Index of the volume to display if the image is 4D.
--interpolation str "nearest" Interpolation method to use. Options: "nearest", "linear".
--scalar_colorbar flag False Whether to show a scalar colorbar. Default is False.
--tractography Path None Optional tractogram(s) to plot with slices. Accepts one or more files.
--tractography_values list of float None Values for coloring each tractogram. Must match the number of tractography files.
--tractography_cmap str "plasma" or "Set1" Matplotlib colormap to use for tractography. Default is "plasma" if values provided, otherwise "Set1".
--tractography_cmap_range float, float 0, 1 Optional range for the colormap. Default is 0 to 1.
--tractography_colorbar flag False Whether to show a colorbar for tractography values. Default is False.

Example Commands

  1. Visualize the Middle Axial Slice Interactively:

    python nifti2png.py my_image.nii.gz --interactive
  2. Save a Coronal Slice to a File:

    python nifti2png.py my_image.nii.gz -s 50 -o coronal --save_path slice_50_coronal.png
  3. Customize Resolution and Intensity Range:

    python nifti2png.py my_image.nii.gz --size 800 600 --value_range 0 1500
  4. Show tractography on a colormap

    python nifti2png.py my_image.nii.gz --tractography my_tractogram1.trk my_tractogram2.trk --tractography_values -0.5 0.7 --tractography_cmap_range -1 1 --tractography_cmap plasma

Troubleshooting

Common Errors and Fixes

Error Cause Solution
FileNotFoundError Input NIfTI file path is incorrect or file missing. Ensure the file exists and the path is correct.
ImportError Required libraries are not installed. Install dependencies using pip.
Image is not displayed Missing display capability in the environment. Use a system with GUI support or save the output. Make sure you use the --interactive flag.
ValueError: Invalid range Incorrect --value_range arguments. Ensure range is within the NIfTI’s intensity values.

Getting Help

If issues persist, refer to the project documentation or open an issue on the project’s GitHub page.


By following these guidelines, you can easily render and save 2D representations of NIfTI images with nifti2png.